感谢您的回答,但这就是我的做法。希望人们可以使用这个: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;
}