使用UIView动画从CATransition实现kCATransitionPush

leo*_*leo 5 iphone cocoa-touch core-animation ios

苹果建议为iOS4及更高版本(此处)使用UIView动画块,但我非常喜欢带有kCATransitionPush效果的旧CATransition。如何使用UIView动画实现kCATransitionPush?我只在可用选项中看到四种类型的视图过渡(向左/向右翻转,向上/向下卷曲)

如果我自己实现,则计划创建一个新视图并在滑入旧视图的同时将其滑入。我不明白为什么苹果不在UIView动画中包含“推”效果。

谢谢!

狮子座

Dan*_*son 4

通过阅读您发布的苹果文档,它似乎还说:

基于块的动画方法(例如 animateWithDuration:animations:)极大地简化了动画的创建。通过一次方法调用,您可以指定要执行的动画以及动画的选项。但是,基于块的动画仅在 iOS 4 及更高版本中可用。如果您的应用程序在早期版本的 iOS 上运行,则必须使用 beginAnimations:context: 和 commitAnimations 类方法来标记动画的开始和结束。

我认为这意味着他们推荐它,因为它极大地简化了动画的创建。它还指出,它不适用于任何没有 ios 4 的设备。如果您知道如何使用 CAAnimation,我会继续使用它。我仍然使用它,没有任何问题:

- (void)switchTwoViews:(UIView *)view1 otherView:(UIView *)view2 direction:(int)directionRL{
view2.frame = CGRectMake(0, 0, 400, 211);
visibleView = view2;
// remove the current view and replace with view1
[view1 removeFromSuperview];
[currentOptionsView addSubview:view2];

// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[animation setDuration:0.3];
[animation setType:kCATransitionPush];
if (directionRL == 0) {
    [animation setSubtype:kCATransitionFromRight];
} else {
    [animation setSubtype:kCATransitionFromLeft];
}

[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[currentOptionsView layer] addAnimation:animation forKey:@"SwitchToView"];
Run Code Online (Sandbox Code Playgroud)

}