2012-01-27 37 views
3

我想按一個按鈕時將圖像轉動190度。這有效,但是當我再次按下按鈕時,動畫需要從最後結束的地方開始,而不是從0開始。所以,每次按下按鈕,動畫都需要變成190度。Xcode圖像旋轉toValue和from價值

它從0開始,每次becouse我fromValue設置爲0

是否有人知道我可以讓fromValue地方開始我的形象結束。我的代碼在這裏。

- (IBAction)buttonPressed 
{ 
    CABasicAnimation *imageRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 

    imageRotation.fromValue = [NSNumber numberWithFloat:0]; 
    imageRotation.toValue = [NSNumber numberWithFloat:((190*M_PI)/ -180)]; 

    imageRotation.duration = 1.5; 
    imageRotation.repeatCount = 1; 

    imageRotation.removedOnCompletion = NO; 
    imageRotation.autoreverses=NO; 
    imageRotation.fillMode = kCAFillModeForwards; 

    [image.layer addAnimation:imageRotation forKey:@"imageRotation"]; 
} 

回答

5

感謝您的回答,但這裏是我如何做到的。希望人們可以使用這樣的:d

我的.h文件看起來像這樣:

#import <UIKit/UIKit.h> 
#import <QuartzCore/QuartzCore.h> 

@interface testViewController : UIViewController { 
    IBOutlet UIImageView *image; 
    NSObject *lastValue; 
} 

- (IBAction)buttonPressed; 

@property(nonatomic, retain) IBOutlet NSObject *lastValue; 

@end 

我.m文件看起來是這樣的:

@synthesize lastValue; 

- (void)viewAnimation0 
{ 
    CABasicAnimation *imageRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 

    imageRotation.fromValue = lastValue; 
    imageRotation.toValue = [NSNumber numberWithFloat:((30*M_PI)/ -180)]; 
    lastValue = imageRotation.toValue; 

    imageRotation.duration = 1.5; 
    imageRotation.repeatCount = 1; 

    imageRotation.removedOnCompletion = NO; 
    imageRotation.autoreverses=NO; 
    imageRotation.fillMode = kCAFillModeForwards; 

    [image.layer addAnimation:imageRotation forKey:@"imageRotation"]; 
} 

- (void)viewAnimation1 
{ 
    CABasicAnimation *imageRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 

    imageRotation.fromValue = lastValue; 
    imageRotation.toValue = [NSNumber numberWithFloat:((60*M_PI)/ -180)]; 
    lastValue = imageRotation.toValue; 

    imageRotation.duration = 1.5; 
    imageRotation.repeatCount = 1; 

    imageRotation.removedOnCompletion = NO; 
    imageRotation.autoreverses=NO; 
    imageRotation.fillMode = kCAFillModeForwards; 

    [image.layer addAnimation:imageRotation forKey:@"imageRotation"]; 
} 

- (void)viewAnimation2 
{ 
    CABasicAnimation *imageRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 

    imageRotation.fromValue = lastValue; 
    imageRotation.toValue = [NSNumber numberWithFloat:((90*M_PI)/ -180)]; 
    lastValue = imageRotation.toValue; 

    imageRotation.duration = 1.5; 
    imageRotation.repeatCount = 1; 

    imageRotation.removedOnCompletion = NO; 
    imageRotation.autoreverses=NO; 
    imageRotation.fillMode = kCAFillModeForwards; 

    [image.layer addAnimation:imageRotation forKey:@"imageRotation"]; 
} 

- (IBAction)buttonPressed 
{ 
    static int counter = 0; 
    switch (++counter) { 

    case 1: 
     [self viewAnimation1]; 
     break; 

    case 2: 
     [self viewAnimation2]; 
     break; 

    default: 
     [self viewAnimation0]; 
     counter = 0; 
     break; 
    } 
    // animateButton.enabled = NO; 
} 
2

你需要做兩件事情:你需要實際設置圖層(而不是設置removedOnCompletion = NO)的旋轉,並且需要不設置fromValue

- (IBAction)buttonPressed 
{ 
    CABasicAnimation *imageRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 

    imageRotation.toValue = [NSNumber numberWithFloat:((190*M_PI)/ -180)]; 

    imageRotation.duration = 1.5; 
    imageRotation.repeatCount = 1; 

    [image.layer setValue:imageRotation.toValue forKey:imageRotation.keyPath]; 
    [image.layer addAnimation:imageRotation forKey:@"imageRotation"]; 
} 

觀看核心動畫精華WWDC 2011視頻來了解爲什麼你需要設置圖層的屬性,而不是設置removedOnCompletion = NO的。