Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode image rotation toValue and fromValue

I want to turn an image 190 degrees when pressing a button. That works but when i press the button again the animation needs to start where it last ended, instead of starting from 0. So everytime i press the button, the animation need to turn a 190 degrees.

It is starting at 0 everytime becouse my fromValue is set to 0.

Does someone know how i can make the fromValue start where my image ends. My code is below 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"];
}
like image 842
Maurice Avatar asked Aug 07 '26 15:08

Maurice


2 Answers

Thanks for your answer, but here is how i did it. Hope people can use this :D

My .h file looks like this:

#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

My .m file looks like this:

@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;
} 
like image 149
Maurice Avatar answered Aug 10 '26 09:08

Maurice


You need to do two things: you need to actually set the rotation of the layer (instead of setting removedOnCompletion = NO), and you need to not set the 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"];
}

Watch the Core Animation Essentials video from WWDC 2011 to understand why you need to set the layer's property instead of setting removedOnCompletion = NO.

like image 28
rob mayoff Avatar answered Aug 10 '26 08:08

rob mayoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!