39

我的设置有问题UIViewAnimationOptionAutoReverse。这是我的代码。

CALayer *aniLayer = act.viewToChange.layer;
[UIView animateWithDuration:2.0 delay:1.0 options:(UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse) animations:^{
                    viewToAnimate.frame = GCRectMake(1,1,100,100);
                    [aniLayer setValue:[NSNumber numberWithFloat:degreesToRadians(34)] forKeyPath:@"transform.rotation"];
                } completion:nil ];

问题是,在动画反转后,视图会跳回到动画块中设置的帧。我希望视图增长和“取消增长”并停止在其原始位置。

有没有不编写两个连续动画的解决方案?

4

4 回答 4

98

你有三个选择。

当您使用这些-[UIView animateWithDuration:…]方法时,您在animations块中所做的更改会立即应用于相关视图。但是,还有一个隐式CAAnimation应用于从旧值动画到新值的视图。当 aCAAnimation在视图上处于活动状态时,它会更改显示的视图,但不会更改视图的实际属性。

例如,如果您这样做:

NSLog(@"old center: %@", NSStringFromCGPoint(someView.center));
[UIView animateWithDuration:2.0 animations: ^{ someView.center = newPoint; }];
NSLog(@"new center: %@", NSStringFromCGPoint(someView.center));

你会看到“老中心”和“新中心”是不同的;新中心将立即反映新点的值。但是,CAAnimation隐式创建的 将导致视图仍显示在旧中心并顺利移动到新中心。动画完成后,它会从视图中移除,然后您切换回仅查看实际模型值。

当您通过UIViewAnimationOptionAutoreverse时,它会影响隐式创建的CAAnimation,但不会影响您对值所做的实际更改。也就是说,如果我们上面的例子有UIViewAnimationOptionAutoreverse定义,那么隐式创建的CAAnimation动画将从 oldCenter 到 newCenter 并返回。然后动画将被删除,我们将切换回查看我们设置的值……它仍然在新的位置

正如我所说,有三种方法可以解决这个问题。第一种是在动画上添加一个完成块来反转它,如下所示:

第一个选项

CGPoint oldCenter = someView.center;
[UIView animateWithDuration:2.0
                 animations: ^{ someView.center = newPoint; }
                 completion: 
   ^(BOOL finished) {
       [UIView animateWithDuration:2.0
                        animations:^{ someView.center = oldCenter; }];
   }];

第二种选择

第二个选项是像你正在做的那样自动反转动画,并将视图设置回它在完成块中的原始位置:

CGPoint oldCenter = someView.center;
[UIView animateWithDuration:2.0
                      delay:0
                    options: UIViewAnimationOptionAutoreverse
                 animations: ^{ someView.center = newPoint; }
                 completion: ^(BOOL finished) { someView.center = oldCenter; }];

但是,这可能会导致动画自动反转完成时间和完成块运行时间之间闪烁,因此它可能不是您的最佳选择。

第三个选项

最后一个选项是直接创建一个CAAnimation。当您实际上不想更改要更改的属性的最终值时,这通常更简单。

#import <QuartzCore/QuartzCore.h>

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.autoreverses = YES;
animation.repeatCount = 1; // Play it just once, and then reverse it
animation.toValue = [NSValue valueWithCGPoint:newPoint];
[someView.layer addAnimation:animation forKey:nil];

请注意,CAAnimation做事的方式永远不会改变视图的实际值;它只是用动画掩盖了实际值。视图仍然认为它在原始位置。(这意味着,例如,如果您的视图响应触摸事件,它仍然会观察这些触摸事件是否在原始位置发生。动画只会改变视图绘制的方式;没有别的。

CAAnimation方式还要求您将其添加到视图的底层CALayer. 如果这让您感到害怕,请随意使用这些-[UIView animateWithDuration:…]方法。使用 可以使用额外的功能CAAnimation,但如果你不熟悉它,链接 UIView 动画或在完成块中重置它是完全可以接受的。事实上,这是完成块的主要目的之一。

所以,你去吧。反转动画并保持原始值的三种不同方法。享受!

于 2011-02-18T17:51:08.943 回答
15

这是我的解决方案。对于 2x 重复,动画 1.5x 并自己完成最后 0.5x 部分:

[UIView animateWithDuration:.3
                      delay:.0f
                    options:(UIViewAnimationOptionRepeat|
                             UIViewAnimationOptionAutoreverse)
                 animations:^{
                     [UIView setAnimationRepeatCount:1.5f];

                     ... animate here ...

                 } completion:^(BOOL finished) {
                         [UIView animateWithDuration:.3 animations:^{

                             ... finish the animation here ....

                         }];
                 }];

不闪,很好用。

于 2012-07-26T13:35:19.330 回答
0

上有一处repeatCount房产CAMediaTiming。我认为您必须创建一个显式CAAnimation对象,并正确配置它。增长和取消增长的重复计数为 2,但您可以对此进行测试。

这句话很长。不过,您需要两个 CAAnimation 对象,一个用于框架,一个用于旋转。

CABasicAnimation *theAnimation;

theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
theAnimation.duration=3.0;
theAnimation.repeatCount=1;
theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSNumber numberWithFloat:0.0];
theAnimation.toValue=[NSNumber numberWithFloat:degreesToRadians(34)];
[theLayer addAnimation:theAnimation forKey:@"animateRotation"];

AFAIK 无法避免对两个动画对象进行编程。

于 2011-02-18T11:13:45.940 回答
0

这是@Rudolf Adamkovič 答案的 Swift 版本,您将动画设置为运行 1.5 次,并在完成块中最后一次运行“反向”动画。

在下面的片段中,我将我的视图在 y 轴上平移了 -10.0 分 1.5 倍。然后在完成块中,我最后一次将我的视图动画化回其原始 y 轴位置 0.0。

UIView.animate(withDuration: 0.5, delay: 0.5, options: [.repeat, .autoreverse], animations: {

    UIView.setAnimationRepeatCount(1.5)
    self.myView.transform = CGAffineTransform(translationX: 0.0, y: -10.0)
            
}, completion: { finished in
            
    UIView.animate(withDuration: 0.5, delay: 0, options: [], animations: {

        self.myView.transform = CGAffineTransform(translationX: 0.0, y: 0.0)

    }, completion: nil) 
})
于 2021-05-25T10:25:27.217 回答