我使用的是创造了一个旋转动画CABasicAnimation
。 它旋转的UIView
超过2秒。 不过,我需要能够阻止它当UIView
感动。 如果我删除动画的看法是在相同的位置作为动画开始前。
这里是我的动画代码:
float duration = 2.0;
float rotationAngle = rotationDirection * ang * speed * duration;
//rotationAngle = 3*(2*M_PI);//(double)rotationAngle % (double)(2*M_PI) ;
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: rotationAngle ];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.delegate = self;
[self.view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
我怎样才能阻止UIView
自转正确的地方是,当它触动? 我知道如何管理触控一部分,但我无法弄清楚如何停止在动画当前的角度视图。
解决方案:我通过获取表示层的角度,去除动画和设置视图的转换解决了这个问题。 下面的代码:
[self.view.layer removeAllAnimations];
CALayer* presentLayer = self.view.layer.presentationLayer;
float currentAngle = [(NSNumber *)[presentLayer valueForKeyPath:@"transform.rotation.z"] floatValue];
self.view.transform = CGAffineTransformMakeRotation(currentAngle);