iOS Swift旋转动画不停留

时间:2016-07-15 21:06:40

标签: ios swift uiimageview

我正在玩旋转UIImageView。旋转动画工作正常,但如果图像返回其原始方向则结束。下面是我用来旋转它的扩展程序:

extension UIView {
func rotate(duration: CFTimeInterval = 1.0, degrees:Double, completionDelegate: AnyObject? = nil) {
    let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
    rotateAnimation.fromValue = 0.0
    let radians = CGFloat(degrees * M_PI / degrees)
    rotateAnimation.toValue = CGFloat(radians)
    rotateAnimation.duration = duration

    if let delegate: AnyObject = completionDelegate {
        rotateAnimation.delegate = delegate
    }
    self.layer.addAnimation(rotateAnimation, forKey: nil)
}

}

我关闭了自动布局。我错过了什么?

3 个答案:

答案 0 :(得分:0)

这些属性应该有所帮助:

rotateAnimation.fillMode = kCAFillModeForwards
rotateAnimation.removedOnCompletion = false

答案 1 :(得分:0)

您还可以使用animateWithDuration为视图设置动画。

func rotate(duration: NSTimeInterval, degrees: CGFloat, completionDelegate: (finished: Bool) -> ()) {
    let radians = CGFloat(degrees * CGFloat(M_PI) / degrees)
    UIView.animateWithDuration(duration, animations: {
        self.transform = CGAffineTransformMakeRotation(radians)
    }, completion: completionDelegate)
}

答案 2 :(得分:0)

在直接核心动画中,你必须实际设置你正在制作动画的属性。单独动画只显示效果,然后弹回到起始值。设置fillModeremovedOnCompletion 可视地工作时,图层上旋转属性的实际值仍然是原始值,而不是您在{{中指定的结束值1}} 动画的属性。最终结果是仅视觉

我建议您使用Jan建议的toValue动画功能,或者,您可以在动画中明确设置属性,如:

UIView