Xcode图像旋转到值和从值
我想在按下按钮时将图像旋转190度。这是可行的,但当我再次按下按钮时,动画需要从上次结束的位置开始,而不是从0开始。所以每次我按下按钮,动画都需要旋转190度 它每次都从0开始,因为my fromValue设置为0 有人知道我如何使fromValue从图像的结尾开始吗。我的代码在下面Xcode图像旋转到值和从值,xcode,image,rotation,cabasicanimation,Xcode,Image,Rotation,Cabasicanimation,我想在按下按钮时将图像旋转190度。这是可行的,但当我再次按下按钮时,动画需要从上次结束的位置开始,而不是从0开始。所以每次我按下按钮,动画都需要旋转190度 它每次都从0开始,因为my fromValue设置为0 有人知道我如何使fromValue从图像的结尾开始吗。我的代码在下面 - (IBAction)buttonPressed { CABasicAnimation *imageRotation = [CABasicAnimation animationWithKeyPath:@
- (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"];
}
您需要做两件事:您需要实际设置层的旋转(而不是设置
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"];
}
观看中的Core Animation Essentials视频,了解为什么需要设置层的属性,而不是设置
removedOnCompletion=NO
谢谢您的回答,但下面是我如何做到的。希望大家能用这个: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
@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;
}