【发布时间】:2012-06-19 10:07:07
【问题描述】:
我正在使用CABasicAnimation 创建的旋转动画。它会在 2 秒内旋转 UIView。但是当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);
【问题讨论】:
-
你把上面的解决方案放在哪里了??我总是得到角度 0.0000
标签: iphone objective-c ios ipad core-animation