2010-01-15 2 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

Можете ли вы отредактировать это и поставить четыре пробела перед каждой строкой кода? что сделает его читаемым. –

ответ

1

Ваша анимация не работает, потому что Core Animation видит, что преобразование на 360 градусов является таким же, как и исходное изображение, поэтому оно ничего не делает.

Самый простой способ справиться с вращением на 360 градусов описан в this answer к this question, где вы создали свой CABasicAnimation анимировать transform.rotation.z свойство от 0 до 2 * пи радиан.

Смежные вопросы