ios如何360度旋转控制

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios如何360度旋转控制相关的知识,希望对你有一定的参考价值。

我想将控制旋转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];

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

答案

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];
另一答案

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

另一答案

试试下面的代码。

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

        self.crossButton.transform = CGAffineTransformMakeRotation(M_PI);

    } completion:nil];
另一答案

斯威夫特3:

使用CABasicAnimation

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的扩展函数,用于处理启动和停止旋转操作:

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")
    }
}

现在使用,UIView.animation闭包:

UIView.animate(withDuration: 0.5, animations: { 
      button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi)) 
}) { (isHalfAnimationComplete) in
    UIView.animate(withDuration: 0.5) { 
       button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi * 2))
    }    
}

以上是关于ios如何360度旋转控制的主要内容,如果未能解决你的问题,请参考以下文章

使用 CSS3 动画将元素旋转到 360 度

Swift UI 动画 360 度旋转

HTML5/CSS3 - 如何制作“无尽”旋转背景 - 360 度全景

360 度图像的 A-Frame 用户滑动效果

css3 实现360度无线旋转

如何旋转GameObject?