Ajouter une animation de vue

J’ai UIView principal où j’affiche des données différentes. Ensuite, je mets un bouton qui affiche la sous-vue comme ceci:

- (IBAction) buttonClicked:(id)sender { UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)]; UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(25,25,50,25)]; newLabel.text = @"some text"; [newView addSubview:newLabel]; [self.view addSubview:newView]; [newLabel release]; [newView release]; } 

newView semble bien, mais il ne s’anime pas, il apparaît juste immédiatement. Comment puis-je append une animation lorsque newView apparaît? Aimez le zoom ou glissez vers le bas ou quelque chose comme ça. Y a-t-il un code simple?

Bonjour Vous pouvez utiliser les animations UIView:

 [newView setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)]; //notice this is OFF screen! [UIView beginAnimations:@"animateTableView" context:nil]; [UIView setAnimationDuration:0.4]; [newView setFrame:CGRectMake( 0.0f, 0.0f, 320.0f, 480.0f)]; //notice this is ON screen! [UIView commitAnimations]; 

Le SDK va maintenant le découvrir pour vous et animer la vue des positions que vous lui avez données. Cela fonctionne pour la plupart des propriétés des UIViews: alpha, scale, bounds, frames, etc.

Il existe également des animations intégrées comme flip et curl:

 [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.0]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:newView cache:YES]; [self.navigationController.view addSubview:settingsView.view]; [UIView commitAnimations]; 

J’espère que cela vous aidera à démarrer 🙂

celui-ci est pour usage recommandé:

 [UIView transitionWithView:containerView duration:0.5 options:UIViewAnimationOptionTransitionCurlUp //change to whatever animation you like animations:^ { [containerView addSubview:subview]; } completion:nil]; 

lien avec le cadre QuarzCore

 #import  CATransition *transition = [CATransition animation]; transition.duration = 1.0; transition.type = kCATransitionPush; //choose your animation [newView.layer addAnimation:transition forKey:nil]; [self.view addSubview:newView]; 

J’ai trouvé que même avec les blocs d’animation et les options, essayer d’animer seul addSubview ne ferait rien pour moi. J’ai trouvé une solution de contournement qui consiste à définir l’alpha de la sous-vue sur 0.0, append la sous-vue, puis animer le paramètre de l’alpha de 0.0 à 1.0. Cela me donne le fondu que je cherchais.

J’espère que ceci aide quelqu’un d’autre.

 UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)]; redView.backgroundColor = [UIColor redColor]; redView.alpha = 0.0; [self.view addSubview:redView]; [UIView animateWithDuration:0.5 animations:^{ redView.alpha = 1.0; }]; 

Faisons cela rapidement.

 var redView = UIView(frame:CGRectMake(20,20,100,100)) redView.backgroundColor = UIColor.redColor redView.alpha = 0.0 view.addSubview(redView) UIView.animateWithDuration(0.25) { () -> Void in redView.alpha = 1.0 } 

L’ajout d’une sous-vue ne peut pas être animé simplement en le plaçant dans un bloc animateWithDuration. Et il ne peut pas être animé en utilisant caché.