我是iPhone动画功能的新手。我写了一个简单的测试程序来旋转一个红色矩形;但是,动画根本不起作用。我的代码出了什么问题?非常感谢...
//.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#define DegreesToRadians(X) (M_PI*X/180.0)
//.m
- (void)viewDidLoad {
[super viewDidLoad];
CGRect theRect = CGRectMake(0,0,100,100);
UIView *theBox = [[UIView alloc] initWithFrame:theRect];
theBox.backgroundColor = [UIColor redColor];
[self.view addSubview:theBox];
CALayer *theLayer = [theBox layer];
[self spinLayer:theLayer];
}
-(CAAnimation *)animationForSpinning{
float radians = DegreesToRadians(360);
CATransform3D theTransform = CATransform3DMakeRotation(radians, 0, 0, 1.0);
CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
theAnimation.toValue = [NSValue valueWithCATransform3D:theTransform];
theAnimation.duration = 2; // 2 seconds
theAnimation.repeatCount = 10000;
theAnimation.cumulative = YES;
return theAnimation;
}
-(void)spinLayer:(CALayer *)theLayer{
CAAnimation *spinningAnimation = [self animationForSpinning];
[theLayer addAnimation:spinningAnimation forKey:@"opacity"];
// trigger the animation
theLayer.opacity = 0.99;
}
答案 0 :(得分:1)
您的动画未运行,因为Core Animation看到360度的变换与原始图像相同,因此它什么都不做。
在this answer到this question中描述了处理360度旋转的最简单方法,您可以在其中设置CABasicAnimation,以便将transform.rotation.z
属性设置为0到2 * pi弧度的动画。