2010-02-25 5 views
49

J'ai UIView principal où j'affiche des données différentes. Puis-je mettre un bouton, qui affiche comme celui-ci sous-vue:addSubview animation

- (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 aucune façon, il apparaît juste immédiatement. Comment puis-je ajouter une sorte d'animation quand newView apparaît? Comme zoom ou glisser vers le bas ou quelque chose comme ça. Y a-t-il un code simple?

Répondre

76

Salut 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 comprendre cela pour vous et animer la vue sur les positions que vous lui a donné. Cela fonctionne pour la plupart des propriétés de UIViews: alpha, échelle, limites, cadres, etc.

Il y a aussi construire dans des animations sous forme papier et boucle:

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.0]; 
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight 
          forView:newView 
          cache:YES]; 

[self.navigationController.view addSubview:settingsView.view]; 
[UIView commitAnimations]; 

Hope this helps à obtenir que vous avez commencé :)

+0

cool, merci, qui a travaillé très bien – Mike

+0

Alors que le premier exemple est techniquement valable, il est mauvaise forme compte tenu des grandes quantités de tailles d'écran dont vous avez besoin pour soutenir aujourd'hui et je recommande soit la réponse de la deuxième ou @ adedoy – Sirens

22
lien

par rapport au cadre QuarzCore

#import <QuartzCore/QuartzCore.h> 

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

+1 Vous pouvez également utiliser des sous-types de transition pour personnaliser davantage la transition.https: //developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CATransition_class/#//apple_ref/doc/constant_group/Common_Transition_Subtypes – anoop4real

79

celui-ci est pour l'usage recommandé:

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

Hey @adedoy comment utiliser l'animation droite sans basculement possible ?? comme pushviewcontroller ............ –

19

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

J'espère que cela aidera 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; 
}]; 
+0

réponse parfaite pour dissoudre dans une vue – Fattie

10

Faisons cela dans swift.

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 
} 

Ajout d'un sous-vue ne peut pas être animé en mettant simplement dans un bloc animateWithDuration. Et il ne peut pas être animé en utilisant caché.

Questions connexes