我需要向 UIButton 添加动画 - 简单的移动动画。当我这样做时,按钮会移动,但该按钮的点击区域仍保持在旧位置:
UIButton *slideButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[slideButton addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
[slideButton setFrame:CGRectMake(50, 50, 50, 50)];
[self.view addSubview:slideButton];
CABasicAnimation *slideAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
[slideAnimation setRemovedOnCompletion:NO];
[slideAnimation setDuration:1];
[slideAnimation setFillMode:kCAFillModeBoth];
[slideAnimation setAutoreverses:NO];
slideAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
slideAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
[slideButton.layer addAnimation:slideAnimation forKey:@"slide"];
我已经看到了在动画结束时更改按钮框架的答案(就像这里UIButton 在 CABasicAnimaton 之后的错误位置接收 touchEvents)但这不是我的情况,因为我应该让用户有可能在单击按钮时单击按钮它正在移动......另外,我不能使用其他方法(如 NSTimer)作为 CABasicAnimation 的替代品,因为我将有一个复杂的路径来移动对象......
感谢您的帮助!