iOS 快速旋转动画不固定

标签 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)
}

}

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

最佳答案

在直接核心动画中,您必须实际设置要设置动画的属性。单独制作动画仅显示效果,然后弹回到起始值。虽然设置 fillModeremovedOnCompletion在视觉上起作用,但图层上旋转属性的实际值仍然是原始值,而不是最终值您在动画toValue属性中指定。最终结果仅是视觉效果

我建议您按照 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)
}

关于iOS 快速旋转动画不固定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38405020/

相关文章:

objective-c - 搜索模板 tvOS

ios - 使用 3D 类型初始化 Metal 纹理

ios - 平移/调整大小/旋转后在 UIImageView 上重新创建相同的转换

ios - 如何拖动具有最小值和最大值的图像?

ios - 有没有一种经典的或很好的方法可以将不同的指令应用于循环的第一个或最后一个元素?

iOS 应用程序因启动时崩溃而被拒绝(无法重现)

ios - iOS 应用程序需要哪些低端网络端口?

ios - 创建收集的图像数组

ios - 在 GameScene.swift 中更改 GameViewController.swift 对象的属性

ios4 - 在 UITableView 中调整 imageView 的大小