你有三个选择。
当您使用这些-[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 动画或在完成块中重置它是完全可以接受的。事实上,这是完成块的主要目的之一。
所以,你去吧。反转动画并保持原始值的三种不同方法。享受!