gpt4 book ai didi

iphone - 使用 CABasicAnimation iPhone 启动和停止动画

转载 作者:行者123 更新时间:2023-12-03 21:19:48 28 4
gpt4 key购买 nike

我想对图像使用动画,在单击按钮时开始并在 5 秒后停止。我使用此代码。

-(IBAction) btnClicked
{
CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
fullRotation.duration = 6;
fullRotation.repeatCount = 1e100f;
fullRotation.delegate = self;
[imgView.layer addAnimation:fullRotation forKey:@"360"];
[imgView.layer setSpeed:3.0];
}

使用此代码动画已启动,但我不知道如何在 5 秒后停止此动画。

最佳答案

甚至不处理CoreAnimation。我认为这可以通过 UIView 动画来实现,它更容易编程。查看 UIView 文档并尝试 block 动画(假设 iOS 4.0。如果没有,请使用旧样式)。

here有关 UIView 动画的文档。设置 transform 属性以使用 CGAffineTransformMakeRotation 进行旋转。

这是一个基于 UIView 的动画旋转:

- (void)viewDidLoad
{
[super viewDidLoad];
UIView *viewToAnimate = [[[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)] autorelease];
viewToAnimate.backgroundColor = [UIColor blueColor];
[self.view addSubview:viewToAnimate];
[UIView animateWithDuration:5 animations:^{
viewToAnimate.transform=CGAffineTransformMakeRotation(M_PI);
}];
}

关于iphone - 使用 CABasicAnimation iPhone 启动和停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5839631/

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