【问题标题】:Spin Animation Resets to Original Image旋转动画重置为原始图像
【发布时间】:2015-08-03 16:29:54
【问题描述】:

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

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

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 方法完成后,图像保持旋转到新的旋转方向,我将不胜感激。谢谢!

【问题讨论】:

    标签: objective-c animation rotation cabasicanimation


    【解决方案1】:

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

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

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

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

    相反,更好的解决方案是在将动画添加到图层时更新您正在制作动画的属性的值。这可能会破坏动画,但如果动画从旧值动画到新值,它将“覆盖”任何模型值是渲染的原因,并且当动画删除时,最终值将匹配模型值.


    有关添加动画时在何处更改模型值以及动画应用方式的更深入讨论,请参阅this questionthis blog post of mine

    【讨论】:

    • 感谢您的帮助 - 并不是每天都有人花时间变得友善。欣赏它,伙计!
    猜你喜欢
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 2016-09-23
    • 2016-04-29
    • 2015-07-24
    相关资源
    最近更新 更多