Swift 完全忽略 UIImageView 的动画

Swift is completely ignoring the animation for UIImageView

动画根本不起作用。图像被添加到屏幕,但不是动画。代码中的图片一般不会添加到viewDidAppear函数中,这里只是为了测试。仍然没有帮助...

下面的函数在 ViewController 中,在页面 ViewController 中加载。

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        let image = UIImage.fontAwesomeIcon(
            name: .bell,
            style: .solid,
            textColor: .white,
            size: CGSize(width: 100, height: 100)
        )

        let imageView = Init(UIImageView(frame: CGRect.zero)) {
            [=11=].contentMode = .scaleAspectFit
            [=11=].translatesAutoresizingMaskIntoConstraints = false
            [=11=].image = image

        }
        self.view.addSubview(imageView)

        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = .infinity
        animation.duration = 7
        animation.fromValue = 0.0
        animation.toValue = -Double.pi * 2
        imageView.layer.add(animation, forKey: nil)

    }

我尝试了不同的动画,但没有一个动画有效。请帮助....

编辑1

let animation = CABasicAnimation(keyPath: "position")
        animation.repeatCount = .infinity
        animation.duration = 7
        animation.autoreverses = true
        animation.fromValue = NSValue(cgPoint: CGPoint.init(x: imageView.center.x - 10, y: imageView.center.y))
        animation.toValue = NSValue(cgPoint: CGPoint.init(x: imageView.center.x + 10, y: imageView.center.y))
        imageView.layer.add(animation, forKey: "position.x")

编辑2: 这也不起作用:


    var imageView: UIImageView?

    override viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        let keyFrame = CAKeyframeAnimation(keyPath: "position")
        let point = image!.layer.position
        keyFrame.values = [NSValue(cgPoint: CGPoint(x: CGFloat(point.x), y: CGFloat(point.y))),
                           NSValue(cgPoint: CGPoint(x: CGFloat(point.x - 10), y: CGFloat(point.y))),
                           NSValue(cgPoint: CGPoint(x: CGFloat(point.x + 10), y: CGFloat(point.y))),
                           NSValue(cgPoint: CGPoint(x: CGFloat(point.x - 10), y: CGFloat(point.y))),
                           NSValue(cgPoint: CGPoint(x: CGFloat(point.x + 10), y: CGFloat(point.y))),
                           NSValue(cgPoint: CGPoint(x: CGFloat(point.x - 10), y: CGFloat(point.y))),
                           NSValue(cgPoint: CGPoint(x: CGFloat(point.x + 10), y: CGFloat(point.y))),
                           NSValue(cgPoint: point)]
        keyFrame.repeatCount = .infinity
        keyFrame.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
        keyFrame.duration = 0.7
        image!.layer.position = point

        image!.layer.add(keyFrame, forKey: keyFrame.keyPath)

    }

    override func loadView() {

        var image = UIImage.fontAwesomeIcon(
            name: .bell,
            style: .solid,
            textColor: .white,
            size: CGSize(width: 2000, height: 2000)
        )

        imageView = Init(UIImageView(frame: CGRect.zero)) {
            [=13=].contentMode = .scaleAspectFit
            [=13=].translatesAutoresizingMaskIntoConstraints = false
            [=13=].image = image

        }
        self.view.addSubview(imageView!)

    }
imageView.layer.add(animation, forKey: nil)

我相信您可能需要在此处添加密钥而不是 nil,因此这一行是

imageView.layer.add(animation, forKey: "transform.rotation")

此外,您的动画密钥可能应该是 "transform.rotation.z"。

transform.rotation 是矩阵而不是值。我相信您仍然可以为整个 transform.rotation 设置动画,但您必须将其设置为 CATransform3D 而不是简单的浮点值。

好的,原来 viewDidAppear 运行太早了,因此没有设置动画。像这样设置它解决了它。 1 秒后调度动画。哪怕是半秒也没关系。只是不能立即运行。

DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
    // animation here
}

如果你问我的话很奇怪,但不管怎样。

所以,不知道为什么我不能直接在 viewDidAppear 中 运行 动画,即使在 LoadView 期间设置了 UIView 项目。但此解决方法有所帮助。