ios如何360度旋转控制

时间:2014-04-01 10:42:48

标签: ios xcode animation uiview rotation

我希望将控制旋转360度。我现在这样做

#define degreesToRadians(x) (M_PI * x / 180.0)

[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    [self.crossButton setTransform:CGAffineTransformRotate(self.crossButton.transform, degreesToRadians(360))];
} completion:nil];

但是没有发生。你知道我做错了吗?

4 个答案:

答案 0 :(得分:3)

旋转变换360度是身份转换,因此 对象根本没有动画。

使用"基本属性动画":

可以达到预期的效果
[CATransaction begin];
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.byValue = @(2 * M_PI); // 360 degrees
rotationAnimation.duration = 0.25;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[self.crossButton.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[CATransaction commit];

更新:如果旋转量不是360度(这样最终位置不一样 作为初始位置),以下应该有效:

[CATransaction begin];
CGFloat angle = 45.0 * M_PI/180.0;
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.byValue = @(angle);
rotationAnimation.duration = 0.25;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
rotationAnimation.removedOnCompletion = YES;
[CATransaction setCompletionBlock:^{
    self.crossButton.transform = CGAffineTransformRotate(self.button.transform, angle);
}];
[self.crossButton.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[CATransaction commit];

答案 1 :(得分:1)

如果您旋转360度,则将物体放回相同的位置。如果要查看动画,请增加延迟并检查。您将找到整个物体移动以获得完美的圆形。

答案 2 :(得分:1)

尝试以下代码。

   [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{

        self.crossButton.transform = CGAffineTransformMakeRotation(M_PI);

    } completion:nil];

答案 3 :(得分:0)

斯威夫特3:

使用 CABasicAnimation

enter code here


这是UIView的扩展函数,用于处理start&停止旋转操作:

var rotationAnimation = CABasicAnimation()
rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")
rotationAnimation.toValue = NSNumber(value: (Double.pi * 2.0))
rotationAnimation.duration = 1.0
rotationAnimation.isCumulative = true
rotationAnimation.repeatCount = 100.0
view.layer.add(rotationAnimation, forKey: "rotationAnimation")


现在使用UIView.animation闭包:

extension UIView {

    func startRotation() {
        let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.fromValue = 0
        rotation.toValue = NSNumber(value: Double.pi * 2.0)
        rotation.duration = 1.0
        rotation.isCumulative = true
        rotation.repeatCount = FLT_MAX
        self.layer.add(rotation, forKey: "rotationAnimation")
    }

    func stopRotation() {
        self.layer.removeAnimation(forKey: "rotationAnimation")
    }
}