2010-01-15 42 views
1

我是全新的iPhone動畫功能。我寫了一個簡單的測試程序來旋轉一個紅色的矩形;但是,動畫根本不起作用。我的代碼有什麼問題?非常感謝......需要幫助,使我的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

你可以編輯它並在每行代碼前面放置四個空格嗎?這將使它可讀。 – 2010-01-15 22:56:47

回答

1

動畫沒有運行,因爲核心動畫看到了改造的360度是一樣的原始圖像,所以它什麼都不做。

處理一個360度旋轉的最簡單方式this answer被描述爲this question,在這裏設置您的CABasicAnimation的transform.rotation.z屬性從0到2 * PI弧度動畫。