How to animate the view from left to right in the iPhone?

How to animate the transition to the left to the right (the same viewing mode). When I click the button, the view should continue from left to right. So please guide me and give some examples of links.

Thanks.

+4
source share
4 answers

Suppose you want to click view2 on the right to replace 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]; 

Then (optionally) implement this method to remove view1 from the view hierarchy:

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

In this animation delegate method, you can also free view1 and set its link to nil, depending on whether you need to maintain it after the transition.

+3
source

To animate from left to right, you can use the code code below

  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
source

Another option is to use blocks for animation with a smaller line of codes and for simplicity:

Here is an example

 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 }]; 
+1
source

You can also add animation from right to left, like this:

  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]; 
0
source

All Articles