UIButton在CABasicAnimaton应用后在错误的位置收到触摸



我需要向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 之后在错误的位置接收触摸事件)但这不是我的情况,因为我应该让用户在按钮移动时单击它......另外,我不能使用其他方法(如NSTimer)作为CABasicAnimation的替代品,因为我将有一个复杂的路径来移动对象...

感谢您的帮助!

好吧,我发现它实际上与CABasicAnimation无关。Coz 动画循环对视图中的图层对象进行动画处理,但无论将哪些属性设置为 CABasicAnimation 对象,视图本身都保持不变。

经过几个小时的谷歌搜索,我发现了迈克尔泰森开发的一个很酷的功能:http://atastypixel.com/blog/key-path-based-property-animation/

我稍微改进了一下 - 添加了:自动反转,重复计数,累积,时间偏移量,按值。瞧!几乎相同的CABasic动画 - 但能够动画帧!

最新更新