Objective c 旋转动画重置为原始图像

Objective c 旋转动画重置为原始图像,objective-c,animation,rotation,cabasicanimation,Objective C,Animation,Rotation,Cabasicanimation,为了好玩,我一直在尝试在objective c中使用Xcode设置一个彩票滚轮类型的对象。到目前为止,我已经能够成功地旋转随机旋转的对象,并停止在车轮上的8个项目之一 问题是,我不知道如何“保存”旋转的动画层。每次旋转控制盘时,动画完成后,它将重置并以原始方向显示图像。下面是我为旋转轮子而设置的代码(注意:在这段代码中,我只设置了一个旋转。这是我用来尝试保留图像的模板。另一段代码位于另一个项目中,它存储停止点并将其校正到这些对象的中心。) ViewController.m #import "Vi

为了好玩,我一直在尝试在objective c中使用Xcode设置一个彩票滚轮类型的对象。到目前为止,我已经能够成功地旋转随机旋转的对象,并停止在车轮上的8个项目之一

问题是,我不知道如何“保存”旋转的动画层。每次旋转控制盘时,动画完成后,它将重置并以原始方向显示图像。下面是我为旋转轮子而设置的代码(注意:在这段代码中,我只设置了一个旋转。这是我用来尝试保留图像的模板。另一段代码位于另一个项目中,它存储停止点并将其校正到这些对象的中心。)

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

#define SPIN_CLOCK_WISE 1
#define SPIN_COUNTERCLOCK_WISE -1

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)spinButton:(id)sender {
    [self spinLayer:_spinImage.layer duration:10 direction:SPIN_CLOCK_WISE];

}


- (void)spinLayer:(CALayer *)inLayer duration:(CFTimeInterval)inDuration
        direction:(int)direction
{
    CABasicAnimation* rotationAnimation;

    // Rotate about the z axis
    rotationAnimation =
    [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

    // Rotate 360 degress, in direction specified
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 22.3 * direction];

    // Perform the rotation over this many seconds
    rotationAnimation.duration = inDuration;

    // Set the pacing of the animation
    rotationAnimation.timingFunction =
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    // Add animation to the layer and make it so
    [inLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

    // Save animation
//%%%%%%%%%%  THIS IS WHERE I AM STUCK!! %%%%%%%%%
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

如果有人能帮我保留旋转的图像,这样一旦spinLayer方法完成,图像就会保持旋转到新的旋转方向,我将不胜感激。谢谢

令人困惑的是,动画只是改变层的外观,而不是层的属性

可以将动画视为在渲染时覆盖属性

如您所见,由于模型值从未更改,因此当动画完成并移除自身时,层将返回渲染该特性的实际值

解决此问题的错误方法是永远保留动画。它确实会生成正确的渲染,但会产生一些不必要的副作用,因为模型值现在“永久”与外观不匹配

相反,更好的解决方案是在将动画添加到层时更新正在设置动画的特性的值。这可能会使动画中断,但如果动画是从旧值设置为新值的动画,则它将“覆盖”渲染的模型值,并且当动画移除时,结束值将与模型值匹配



有关添加动画时在何处更改模型值以及动画应用方式的更深入讨论,请参见和

谢谢你的帮助-这里不是每天都有人花时间做好人。谢谢你,伙计!