How to animate the view Left to Right in iPhone?

2019-05-01 04:29发布

How to animate the view transition left to right(Similar push view). When i click the button, the view should be transited from left to right. So please guide me and give some sample links.

Thanks.

4条回答
再贱就再见
2楼-- · 2019-05-01 05:02

To animate from left to right you can use the below block of 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];
查看更多
爷的心禁止访问
3楼-- · 2019-05-01 05:13

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];
查看更多
Root(大扎)
4楼-- · 2019-05-01 05:16

Suppose that you want to push view2 from 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 that animation delegate method you may also want to release view1 and set its reference to nil, depending on whether you need to keep it around after the transition.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-05-01 05:23

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

Here is the sample

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
}];
查看更多
登录 后发表回答