我需要为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 receives touchEvents at the wrong position after a CABasicAnimaton)但是这不是我的情况,因为我应该让用户在按钮移动时点击该按钮...此外,我不能使用其他方法(如NSTimer)作为CABasicAnimation的替代品,因为我将有一个复杂的路径来移动对象......
感谢您的帮助!
答案 0 :(得分:0)
好吧,我发现它不太可能与CABasicAnimation有关。 Coz动画循环为视图内的图层对象设置动画,但无论将哪些属性设置为CABasicAnimation对象,视图本身都保持不变。
经过几个小时的谷歌搜索后,我发现了MICHAEL TYSON开发的一个很酷的功能: http://atastypixel.com/blog/key-path-based-property-animation/
我改进了一点 - 添加:autoreverse,repeatCount,cumulative,timeOffset,byValue ..瞧!几乎相同的CABasicAnimation - 但能够动画帧!