1

我是 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;
}
4

1 回答 1

1

您的动画没有运行,因为 Core Animation 看到 360 度的变换与您的原始图像相同,所以它什么也不做。

此问题的答案中描述了处理 360 度旋转的最简单方法,您可以在其中设置 CABasicAnimation 以将属性设置为 0 到 2 * pi 弧度的动画。transform.rotation.z

于 2010-01-15T23:02:31.307 回答