你有三个选择。
当您使用-[UIView animateWithDuration:…] 方法时,您在animations 块中所做的更改会立即应用于相关视图。但是,还有一个隐含的CAAnimation 应用于从旧值动画到新值的视图。当CAAnimation 在视图上处于活动状态时,它会更改显示的 视图,但不会更改视图的实际属性。
例如,如果你这样做:
NSLog(@"old center: %@", NSStringFromCGPoint(someView.center));
[UIView animateWithDuration:2.0 animations: ^{ someView.center = newPoint; }];
NSLog(@"new center: %@", NSStringFromCGPoint(someView.center));
你会看到'老中心'和'新中心'是不同的;新中心将立即反映 newPoint 的值。但是,隐式创建的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 动画或在完成块中重置它是完全可以接受的。事实上,这是完成块的主要目的之一。
那么,就这样吧。反转动画并保持原始值的三种不同方法。尽情享受吧!