1

(

- (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...
}

)

我旋转标签,现在我想取消它,我在网站上搜索了可能的问题,发现了两个解决方案,我尝试了,但这两个解决方案都不起作用..

4

1 回答 1

1

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

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

- (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

于 2013-07-16T16:11:23.767 回答