Swift完全忽略了UIImageView的动画

时间:2019-08-30 17:32:28

标签: swift animation uiimageview core-animation

动画根本无法正常工作。图像已添加到屏幕,但没有动画。代码中的图像通常不添加到viewDidAppear函数中,这只是为了测试。还是没有帮助...

以下功能位于加载在PageViewController中的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)) {
            $0.contentMode = .scaleAspectFit
            $0.translatesAutoresizingMaskIntoConstraints = false
            $0.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)

    }

我尝试了不同的动画,但是没有一个动画可以工作。请帮助。...

EDIT1

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")

EDIT2: 这也不起作用:


    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)) {
            $0.contentMode = .scaleAspectFit
            $0.translatesAutoresizingMaskIntoConstraints = false
            $0.image = image

        }
        self.view.addSubview(imageView!)

    }

2 个答案:

答案 0 :(得分:0)

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

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

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

另外,您的动画关键点应该是“ transform.rotation.z”。

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

答案 1 :(得分:-1)

好吧,原来viewDidAppear太早运行了,因此没有设置动画。这样设置可以解决它。 1秒后发送动画。即使半秒钟也可以。只是无法立即运行。

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

如果你问我,那很奇怪,但是不管怎么说。

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