2012-01-27 12 views
3

ボタンを押したときに画像を190度回転させたいとします。それはうまくいきますが、ボタンをもう一度押すと、0から始まるのではなく、最後に終了した場所からアニメーションを開始する必要があります。ボタンを押すたびにアニメーションを190度回転させる必要があります。Xcode画像の回転toValueおよびfromValue

それは私の場合、fromValue becouse 0たびに開始されたが、誰かが私は私のイメージはどこで終了場合、fromValueを開始させることができる方法を知っています0

に設定されています。私のコードはここにあります。

- (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ファイルには、次のようになります。

@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; 
} 

#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ファイルは次のようになります

2

次の2つのことを行う必要があります:あなたは、実際に(代わりにremovedOnCompletion = NOを設定する)層の回転を設定する必要があり、あなたがfromValueを設定しないようにする必要があります。あなたがremovedOnCompletion = NOを設定するのではなく、レイヤーのプロパティを設定する必要がある理由

- (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からCore Animationのエッセンシャルビデオを理解します。