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

例如, 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];

最好的方法是什么?如果您也能提供一个片段,我们将不胜感激。

笔记: 我一直无法让第二种方法正常工作。

有帮助吗?

解决方案

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

  

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

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

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

其他提示

我一直在使用后者来制作很多不错的轻量级动画。您可以使用它交叉淡化两个视图,或者将一个视图淡入另一个视图,或淡出它。您可以像横幅一样拍摄另一个视图,您可以使视图拉伸或缩小...我从 beginAnimation / commitAnimations 中获得了很多里程。

不要以为你所能做的就是:

[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,框架甚至是视图的大小。玩转。你可能会对它的能力感到惊讶。

差异似乎是动画需要的控制量。

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

UIView 类方法是常见动画的便捷方法,但比 CATransition 更受限制。例如,只有四种可能的过渡类型(向左翻转,右翻,向上卷曲,向下卷曲)。如果你想进行淡入淡出,你必须深入研究 CATransition的淡入淡出过渡,或者设置 UIView 的alpha的显式动画。 / p>

请注意,Mac OS X上的 CATransition 将允许您指定任意 CoreImage 过滤器以用作过渡,但就像现在一样,您无法执行此操作iPhone,缺少 CoreImage

我们可以使用这个简单的代码在ios 5中为图像添加动画效果。

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!");
    }];

UIView 文档中,阅读 ios4 +

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

无论如何,“阻止”方法现在是首选。我将在下面解释这个简单的块。

考虑下面的截图。bug2 和 bug 3 是 imageView。下面的动画描述了延迟 1 秒后持续时间为 1 秒的动画。bug3 从其中心移动到 bug2 的中心。动画完成后,将记录“中心动画完成!”。

-(void)centerAnimation:(id)sender
{
NSLog(@"Center animation triggered!");
CGPoint bug2Center = bug2.center;

[UIView animateWithDuration:1
                      delay:1.0
                    options: UIViewAnimationCurveEaseOut
                 animations:^{
                     bug3.center = bug2Center;
                 } 
                 completion:^(BOOL finished){
                     NSLog(@"Center Animation Done!");
                 }];
}

希望是干净的!!!

我在这个链接中找到了一个很好的教程。希望这对某些人有所帮助。

uiview-animation-tutorial

这是代码为平滑动画,可能对许多开发人员有用。
我在本教程中找到了这段代码。

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.

让我们试试看看Swift 3 ......

UIView.transition(with: mysuperview, duration: 0.75, options:UIViewAnimationOptions.transitionFlipFromRight , animations: {
    myview.removeFromSuperview()
}, completion: nil)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top