Xcode Image Rotating Tovalue and FromValue

I want to rotate the image by 190 degrees when I press the button. This works, but when I press the button again, the animation needs to start from where it ends last, not from 0. So every time I press the button, the animation needs to rotate 190 degrees.

Because my fromValue is set to 0, it starts from 0 every time.

Does anyone know me? How to make the fromValue of the end of my image start. My code is here.

- (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"];
}

Thank you for your answer, but this is how I did it. Hope people can use this: D

My .h file looks like this:

#import 
#i mport

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

- (IBAction)buttonPressed;

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

@end

My .m file As shown below:

@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;
im ageRotation.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;
}

I want to rotate the image by 190 degrees when I press the button. This works, but when I press it again When the button is pressed, the animation needs to start from the last end, not from 0. So every time the button is pressed, the animation needs to rotate 190 degrees.

Because my fromValue is set to 0, So every time it starts from 0.

Does anyone know how I can make my image end fromValue start. My code is here.

- ( IBAction)buttonPressed
{
CABasicAnimation *imageRotation = [CABasicAnimation animationWithKey Path:@"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"];
}

Thank you for your answer, but this is how I did it. Hope people can use this: D

My .h file looks like this:

p>

#import 
#import

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

- (IBAction)buttonPressed;

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

@end

My .m file looks like this:

@synthesize lastValue;

- (void)view Animation0
{
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.re peatCount = 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;
}

Leave a Comment

Your email address will not be published.