如何在iPhone中从左到右动画视图?

时间:2010-12-08 12:52:24

标签: iphone animation uiview

如何从左到右动画视图过渡(类似的推视图)。单击按钮时,视图应从左向右移动。所以请指导我并提供一些示例链接。

感谢。

4 个答案:

答案 0 :(得分:3)

假设您要从右侧推送view2以替换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];

然后(可选)实现此方法以从视图层次结构中删除view1:

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

在该动画委托方法中,您可能还想释放view1并将其引用设置为nil,具体取决于您是否需要在转换后保留它。

答案 1 :(得分:1)

要从左到右制作动画,您可以使用下面的代码块

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

答案 2 :(得分:1)

另一种选择是使用较少代码行的动画块并简化:

以下是样本

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
}];

答案 3 :(得分:0)

你也可以像这样从右到左添加动画:

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