gpt4 book ai didi

ios - 如何取消 UIView 的旋转 block 动画

转载 作者:行者123 更新时间:2023-11-29 13:09:48 29 4
gpt4 key购买 nike

(

- (void)viewDidLoad
{
[super viewDidLoad];
label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];
label.layer.cornerRadius = 5.0f;
label.text = @"hello world";
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
[label release];
[self startAnimation];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 60, 30);
[btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}

- (void)startAnimation
{
CGAffineTransform transForm = CGAffineTransformMakeRotation(angel * M_PI/180.0f);
[UIView animateWithDuration:0.1f delay:0.0f options:UIViewAnimationOptionCurveLinear animations:^(void){
label.transform = transForm;
} completion:^(BOOL finished) {
NSLog(@"1");
angel = angel + 5;
[self startAnimation];
}];
}

- (void)btnPressed:(id)sender
{
//method 1 :[label.layer removeAllAnimations]; not work...
//method 2 : CGAffineTransform transForm = CGAffineTransformMakeRotation(M_PI/180.0f);
//label.transform = transForm; not work...
}

)

我旋转了标签,现在我想取消它,我在站点中搜索了可能的问题,找到了两个解决方案,我试过了,但是两个解决方案都不起作用..

最佳答案

当您使用 [label.layer removeAllAnimations] 时动画不会停止因为你调用[self startAnimation]无论 finished 的值如何多变的。这会导致动画继续,即使您取消了动画也是如此。

您应该将动画完成 block 更改为以下内容:

- (void)startAnimation
{
CGAffineTransform transForm = CGAffineTransformMakeRotation(angel * M_PI/180.0f);
[UIView animateWithDuration:0.1f delay:0.0f options:UIViewAnimationOptionCurveLinear animations:^(void){
label.transform = transForm;
} completion:^(BOOL finished) {
if (finished) {
NSLog(@"1");
angel = angel + 5;
[self startAnimation];
}
}];
}

使用 [label.layer removeAllAnimations]btnPressed

关于ios - 如何取消 UIView 的旋转 block 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17681050/

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