2010-12-08 3 views

Répondre

3

Supposons que vous souhaitiez pousser view2 à partir de la droite pour remplacer view1.

// Set up view2 
view2.frame = view1.frame; 
view2.center = CGPointMake(view1.center.x + CGRectGetWidth(view1.frame), view1.center.y); 
[view1.superview addSubview: view2]; 
// Animate the push 
[UIView beginAnimations: nil context: NULL]; 
[UIView setAnimationDelegate: self]; 
[UIView setAnimationDidStopSelector: @selector(pushAnimationDidStop:finished:context:)]; 
view2.center = view1.center; 
view1.center = CGPointMake(view1.center.x - CGRectGetWidth(view1.frame), view1.center.y); 
[UIView commitAnimations]; 

Ensuite (en option) mettre en œuvre cette méthode pour supprimer View1 de la hiérarchie de la vue:

- (void) pushAnimationDidStop: (NSString *) animationID finished: (NSNumber *) finished context: (void *) context { 
    [view1 removeFromSuperview]; 
} 

Dans cette méthode déléguée d'animation vous pouvez également libérer view1 et définir sa référence à zéro, selon le si vous devez le garder après la transition.

+0

Merci pour votre réponse. Merci beaucoup ....... – Pugal

+0

Je sais que c'est vieux mais ... Im essayant de suivre la logique. Im un peu confus quant à ce que cela fait. Quelqu'un peut-il l'expliquer? – marciokoko

+0

@marciokoko Ce code place view2 à droite de view1, anime la diapositive des vues vers la gauche (de sorte que view2 remplace view1) et supprime view1 une fois l'animation terminée. Ils appellent cela une «transition poussée». – Costique

1

Animer de gauche à droite, vous pouvez utiliser le bloc ci-dessous code

CATransition *transition = [CATransition animation]; 
    transition.duration = 0.4; 
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    transition.type = kCATransitionPush; 
    transition.subtype = kCATransitionFromLeft; 
    [self.view.window.layer addAnimation:transition forKey:nil]; 
    [self presentViewController:YOUR_VIEWCONTROLLER animated:YES completion:nil]; 
1

Une autre option est d'utiliser des blocs pour l'animation avec moins ligne de codes et pour la simplicité:

Voici l'échantillon

CGRect viewLeftRect;//final frames for left view 
CGRect viewRightRect;//final frames for right view 

[UIView animateWithDuration:0.3f animations:^{ 
    [viewLeft setFrame:viewLeftRect]; 
    [viewRight setFrame:viewRightRect]; 
} completion:^(BOOL finished) { 
    //do what ever after completing animation 
}]; 
0

Vous pouvez également ajouter une animation de droite à gauche comme ceci:

scrAnimation.frame=CGRectMake(248, 175, 500, 414); //your view start position 

    [UIView animateWithDuration:0.5f 
          delay:0.0f 
         options:UIViewAnimationOptionBeginFromCurrentState 
        animations:^{ 
         [scrAnimation setFrame:CGRectMake(0, 175, 500, 414)]; // last position 
        } 
        completion:nil]; 
Questions connexes