顺时针图像旋转动画

问题描述 投票:1回答:2

我有一个动画,它应该不断旋转图像。但它有几个问题。速度很奇怪,尽管我已经设定它不断重复,你可以看到它是如何开始,停止然后重复的。哪个不应该发生。应该不间断旋转。另外,另一个问题是当动画停止时,图像由于某种原因向左移动。

这是我的代码:

func animateLogo()
{
    UIView.animate(withDuration: 6.0, delay: 0.0, options: .repeat, animations: {
        self.logo.transform = CGAffineTransform(rotationAngle: ((180.0 * CGFloat(Double.pi)) / 180.0))
    }, completion: nil)
}
ios swift uiviewanimation cgaffinetransform
2个回答
3
投票

试试这个

func rotateView(targetView: UIView, duration: Double = 1.0) {
    UIView.animate(withDuration: duration, delay: 0.0, options: .curveLinear, animations: {
        targetView.transform = targetView.transform.rotated(by: CGFloat(M_PI))
    }) { finished in
        self.rotateView(targetView: YOUR_LOGO, duration: duration)
    }
}

如何使用

self.rotateView(targetView: YOUR_LOGO, duration: duration)

0
投票

在iOS中,坐标系被翻转。因此,顺便提一下你的学位。这意味着通过270°将为您提供一个角度,相当于标准坐标系中的90°。记住这一点并相应地提供所需的角度。

考虑以下方法。

1)方便扩展角度

postfix operator °

protocol IntegerInitializable: ExpressibleByIntegerLiteral {
    init (_: Int)
}

extension Int: IntegerInitializable {
    postfix public static func °(lhs: Int) -> CGFloat {
        return CGFloat(lhs) * .pi / 180
    }
}

extension CGFloat: IntegerInitializable {
    postfix public static func °(lhs: CGFloat) -> CGFloat {
        return lhs * .pi / 180
    }
}

2)使用CABasicAnimation旋转到任何角度:

extension UIView {
    func rotateWithAnimation(angle: CGFloat, duration: CGFloat? = nil) {
        let pathAnimation = CABasicAnimation(keyPath: "transform.rotation")
        pathAnimation.duration = CFTimeInterval(duration ?? 2.0)
        pathAnimation.fromValue = 0
        pathAnimation.toValue = angle
        pathAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        self.transform = transform.rotated(by: angle)
        self.layer.add(pathAnimation, forKey: "transform.rotation")
    }
}

用法:

override func viewDidAppear(_ animated: Bool) {
    // clockwise
    myView.rotateWithAnimation(angle: 90°)

    // counter-clockwise
    myView.rotateWithAnimation(angle: -270°) 

}

传递负值将逆时针旋转。

参考 - What's the correct way to rotate a UIImage 270 degrees clockwise in Swift 3?

© www.soinside.com 2019 - 2024. All rights reserved.