3

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

fromValue が 0 に設定されているため、毎回 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"];
}
4

2 に答える 2

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;
} 
于 2012-01-28T00:19:34.517 に答える
2

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のCore Animation Essentialsビデオを見て、を設定する代わりにレイヤーのプロパティを設定する必要がある理由を理解してください。removedOnCompletion = NO

于 2012-01-27T16:03:44.723 に答える