【发布时间】: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