Xcode 图像旋转 toValue 和 fromValue

发布于 2024-12-29 03:30:01 字数 748 浏览 0 评论 0原文

我想在按下按钮时将图像旋转 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"];
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独陪着我 2025-01-05 03:30:01

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

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;
} 
北方。的韩爷 2025-01-05 03:30:01

您需要做两件事:您需要实际设置图层的旋转(而不是设置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"];
}

观看 WWDC 2011 中的核心动画要点视频来了解为什么您需要设置图层的属性而不是设置removedOnCompletion = NO

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文