我有一个UIButton
,当我点击按钮时,它会调用动画方法。但是当我再次点击时,动画会变得奇怪,它会快速移动。单击按钮时如何重建动画?感谢。
我试过[self.view.layer removeAllAnimations]
,但没有工作。
这是我的按钮
- (void)viewDidLoad {
...
[_go_button addTarget:self action:@selector(animation_go:) forControlEvents:UIControlEventTouchUpInside];
...
}
这是animation_go:
方法
- (void)animation_go:(id)sender
{
...
for (int start = 0; start < end; start++) {
// that will create four uiview and add to self.view
}
_thread = [[NSThread alloc] initWithTarget:self selector:@selector(animation) object:nil];
[_thread start];
}
这是animation
方法
- (void)animation
{
[UIView beginAnimations:@"Move randomly" context:nil];
[UIView setAnimationDuration:0.06];
CGPoint squarePostion1 = [self return_point:_first_view add_view:_add_first_view tag:1];
_add_first_view.center = squarePostion1;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animation)];
[UIView commitAnimations];
}
答案 0 :(得分:0)
改为使用CABasicAnimation:
CABasicAnimation* translationAnimation;
translationAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
translationAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(266, 266)];
translationAnimation.duration = 1;
translationAnimation.cumulative = YES;
translationAnimation.repeatCount = HUGE_VALF;
translationAnimation.removedOnCompletion = NO;
translationAnimation.fillMode = kCAFillModeForwards;
[self.label.layer addAnimation:translationAnimation forKey:@"positionAnimation"];
此代码为图层属性提供基本的单关键帧动画功能。您还可以根据Apple的技术说明QA1673实现暂停/开始图层动画功能。