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 度の回転を処理する最も簡単な方法は、この質問に対するこの回答で説明されています。ここでは、プロパティを 0 から 2 * pi ラジアンにアニメートするように CABasicAnimation を設定します。transform.rotation.z

于 2010-01-15T23:02:31.307 に答える