繁体   English   中英

iOS Swift旋转动画不固定

[英]iOS Swift Rotation Animation Not Staying Put

我在玩旋转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)
}

}

我的自动版式已关闭。 我想念什么?

这些属性应有助于:

rotateAnimation.fillMode = kCAFillModeForwards
rotateAnimation.removedOnCompletion = false

您还可以使用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)
}

在直接的Core Animation中,您必须实际设置要设置动画的属性。 单独进行动画处理仅显示效果,然后弹出回到初始值。 设置fillModeremovedOnCompletion在视觉上起作用时 ,图层上rotation属性的实际值将保持原始值,而不是您在animationtoValue属性中指定的结束值。 最终结果只是视觉上的

我建议您按照Jan的建议使用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)

    // Actually set the layer's property
    self.layer.transform = CATransform3DMakeRotation(radians, 0.0, 0.0, 1.0)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM