gpt4 book ai didi

Xcode 图像旋转 toValue 和 fromValue

转载 作者:行者123 更新时间:2023-12-01 10:07:25 24 4
gpt4 key购买 nike

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

最佳答案

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

关于Xcode 图像旋转 toValue 和 fromValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9034934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com