我有一个使用 CABasicAnimation 编写的动画代码,如下所示:
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
[anim setDelegate:self];
anim.fromValue = [NSValue valueWithCGPoint:img.center];
if (newValue == 0) {
imgCentre.y = centerStart.y - 11 * kCounterDigitDiff;
anim.toValue = [NSValue valueWithCGPoint:imgCentre];
} else
anim.toValue = [NSValue valueWithCGPoint:imgCentre];
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
anim.duration = 0.3;
[img.layer addAnimation:anim forKey:@"rollLeft"];
img.frame = frame;
由于某些原因,我希望应用相同的动画,但使用以下类型的方法:
[UIView animateWithDuration:0.3 delay:0.0 options:(UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseOut) animations:^{
...
} completion:^(BOOL finished) {
...
}
我想出的是如下:
[img setCenter:imgCentre];
[UIView animateWithDuration:0.3 delay:0.0 options:(UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseOut) animations:^{
if (newValue == 0) {
CGPoint newPoint= CGPointMake(imgCentre.x, centerStart.y - 11 * kCounterDigitDiff);
[img setCenter:newPoint];
} else{
[img setCenter:imgCentre];
}
} completion:^(BOOL finished) {
// animation completion code
}];
img.frame = frame;
整体看起来不错,但我对转换有点悲观?有什么我错过的吗?
最佳答案
你的想法是正确的。但是,与 CAAnimations 不同的是,您不必将属性设置为最终值。 UIView 动画调用会为您处理所有这些。
UIView 动画通常比 CAAnimation 使用起来更简单、更直观。
关于ios - 从 CABasicAnimation 转换代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23033438/