Поворот изображения XCode в Value и fromValue - PullRequest
3 голосов
/ 27 января 2012

Я хочу повернуть изображение на 190 градусов при нажатии кнопки.Это работает, но когда я снова нажимаю кнопку, анимация должна начинаться там, где она в последний раз заканчивалась, вместо того, чтобы начинаться с 0. Поэтому каждый раз, когда я нажимаю кнопку, анимация должна поворачиваться на 190 градусов.

Он начинается с 0 каждый раз, потому что мое fromValue установлено на 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"];
}

Ответы [ 2 ]

5 голосов
/ 28 января 2012

Спасибо за ваш ответ, но вот как я это сделал. Надеюсь, что люди могут использовать это: 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;
} 
2 голосов
/ 27 января 2012

Вам нужно сделать две вещи: вам нужно фактически установить вращение слоя (вместо установки 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 видео с WWDC 2011 , чтобы понять, почему вам нужно установить свойство слоя вместо установки removedOnCompletion = NO.

...