I'm brand new in iPhone animation feature. I wrote a simple testing program to spin a red rectangle; however, the animation doesn't act at all. What's wrong with my code? Thanks a lot...
//.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;
}