iPhone UIView动画最佳实践

iPhone UIView动画最佳实践,iphone,ios,cocoa-touch,uiview,core-animation,Iphone,Ios,Cocoa Touch,Uiview,Core Animation,在iPhone上设置视图转换动画的最佳实践是什么 例如,apple的ViewTransitions示例项目使用如下代码: CATransition *applicationLoadViewIn = [CATransition animation]; [applicationLoadViewIn setDuration:1]; [applicationLoadViewIn setType:kCATransitionReveal]; [applicationLoadViewIn setTimingF

在iPhone上设置视图转换动画的最佳实践是什么

例如,apple的
ViewTransitions
示例项目使用如下代码:

CATransition *applicationLoadViewIn = [CATransition animation];
[applicationLoadViewIn setDuration:1];
[applicationLoadViewIn setType:kCATransitionReveal];
[applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[[myview layer] addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];
但也有一些代码片段漂浮在网络上,如下所示:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];
[myview removeFromSuperview];
[UIView commitAnimations];
最好的方法是什么?如果您也能提供一个片段,我们将不胜感激


注意:我无法让第二种方法正常工作

区别似乎在于您对动画所需的控制量

cattransition
方法为您提供了更多的控制,因此需要设置更多的内容,例如计时功能。作为一个对象,您可以存储它以备将来使用,重构使所有动画都指向它以减少重复代码,等等

UIView
类方法是常见动画的方便方法,但比
cattransition
更为有限。例如,只有四种可能的过渡类型(向左翻转、向右翻转、向上卷曲、向下卷曲)。如果您想进行淡入,您必须深入到
CATTransition的
fade过渡,或者设置
UIView的alpha的显式动画


注意,Mac OS X上的
cattransition
可以让你指定一个任意的
CoreImage
过滤器作为过渡,但是现在你不能在iPhone上这样做,因为iPhone缺少
CoreImage

我已经在很多漂亮的轻量级动画中使用了后者。您可以使用它使两个视图交叉淡入淡出,或使一个视图在另一个视图前面淡入淡出,或使其淡出。你可以像横幅一样拍摄另一个视图,你可以使视图拉伸或收缩。。。我从
beginAnimation
/
committeanimation
中获得了很多收益

不要以为你能做的就是:

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];
以下是一个示例:

[UIView beginAnimations:nil context:NULL]; {
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelegate:self];
    if (movingViewIn) {
// after the animation is over, call afterAnimationProceedWithGame
//  to start the game
        [UIView setAnimationDidStopSelector:@selector(afterAnimationProceedWithGame)];

//      [UIView setAnimationRepeatCount:5.0]; // don't forget you can repeat an animation
//      [UIView setAnimationDelay:0.50];
//      [UIView setAnimationRepeatAutoreverses:YES];

        gameView.alpha = 1.0;
        topGameView.alpha = 1.0;
        viewrect1.origin.y = selfrect.size.height - (viewrect1.size.height);
        viewrect2.origin.y = -20;

        topGameView.alpha = 1.0;
    }
    else {
    // call putBackStatusBar after animation to restore the state after this animation
        [UIView setAnimationDidStopSelector:@selector(putBackStatusBar)];
        gameView.alpha = 0.0;
        topGameView.alpha = 0.0;
    }
    [gameView setFrame:viewrect1];
    [topGameView setFrame:viewrect2];

} [UIView commitAnimations];
如您所见,您可以使用alpha、帧甚至视图的大小。到处玩。您可能会对它的功能感到惊讶。

来自的关于
beginAnimations:context:
方法的部分:

iPhone OS 4.0及更高版本不鼓励使用此方法。您应该改用基于块的动画方法

例如基于Tom评论的基于块的动画

[UIView transitionWithView:mysuperview 
                  duration:0.75
                   options:UIViewAnimationTransitionFlipFromRight
                animations:^{ 
                    [myview removeFromSuperview]; 
                } 
                completion:nil];

ui视图
文档中,阅读有关ios4+

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion

我们可以使用这个简单的代码在iOS5中设置图像动画

CGRect imageFrame = imageView.frame;
imageFrame.origin.y = self.view.bounds.size.height;

[UIView animateWithDuration:0.5
    delay:1.0
    options: UIViewAnimationCurveEaseOut
    animations:^{
        imageView.frame = imageFrame;
    } 
    completion:^(BOOL finished){
        NSLog(@"Done!");
    }];
不管怎样,“块”方法现在是首选。我将解释下面的简单模块

考虑一下下面被剪掉的部分。bug2和Bug3是图像视图。下面的动画描述了延迟1秒后持续1秒的动画。bug3从其中心移动到bug2的中心。动画完成后,将记录“中心动画完成!”


希望那是干净的

以下是平滑动画的代码,可能对许多开发人员都有帮助。


让我们试试看Swift 3

UIView.transition(with: mysuperview, duration: 0.75, options:UIViewAnimationOptions.transitionFlipFromRight , animations: {
    myview.removeFromSuperview()
}, completion: nil)

所以清楚地说,这意味着使用第一种方法(CATTransition),而不是第二种方法?是的,这是第一种方法(CATTransition)。@Steven N,不,这意味着使用基于
UIView
的块动画。它们本质上与
beginAnimations
和friends相同,但使用了块/闭包功能。是的,Yar是对的。以下是块动画的外观:
[UIView transitionWithView:mysuperview持续时间:0.75选项:UIView动画TransitionFlipFromRight动画:^{[myview removeFromSuperview];}完成:nil]
有关详细信息,请查看UIView文档。希望支持3.1.3手机的情况除外。请注意,这是iOS 2.0时代的建议。如果您使用的是iOS 4.0或更高版本,请参阅Rafael Vega关于基于块的方法的回答。还要注意的是,CoreImage现在从iOS 5开始提供,但只有一些功能。我相信你可以在动画中使用CoreImage转换,但你不能在iOS(5)上制作自定义CoreImage过滤器。你将CGPoint bug3.center指定给bug3Center,然后将CGPoint bug2.center指定给同一变量bug3Center?你为什么这么做?哎呀!!伙计们,这是个打字错误。现在更新。这在iOS 4中也可用,称为“基于块的”动画。它不限于iOS 5及更高版本。
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[animation setAutoreverses:YES];
[animation setFromValue:[NSNumber numberWithFloat:1.3f]];
[animation setToValue:[NSNumber numberWithFloat:1.f]];
[animation setDuration:2.f];
[animation setRemovedOnCompletion:NO];

[animation setFillMode:kCAFillModeForwards];
[[self.myView layer] addAnimation:animation forKey:@"scale"];/// add here any Controller that you want t put Smooth animation.
UIView.transition(with: mysuperview, duration: 0.75, options:UIViewAnimationOptions.transitionFlipFromRight , animations: {
    myview.removeFromSuperview()
}, completion: nil)