ios - 从 CABasicAnimation 转换代码

标签 ios ios7 core-animation uiviewanimation cabasicanimation

我有一个使用 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/

相关文章:

iphone - 如何将UIView的部分转换为UIImage并在imageView中显示

iphone - 在 UITableView 中以编程方式移动带动画的行

ios - 如何通过代码获取 iOS 设备的 GPU 信息

ios - 在 iOS 上,为什么设置层的 sublayerTransform 会使其自身像 CATranformLayer 一样工作?

ios - 通过蓝牙将 GPS 位置发送到 iOS 设备

ios - UICollectionView 不是从 iOS 中的 Y 位置 0 开始

ios - 在 UICollectionView 中显示一行 - Swift

ios - 使用 Reachability 类检查 ios 中整个应用程序的事件互联网连接

具有来自文件的多个段落样式的 iOS7 UITextView

objective-c - UITableView 中的圆角 (iOS7)