简体   繁体   English

UIImageView 没有动画

[英]UIImageView is not animating

I am trying to have a view in which 3 dots would animate infinity and here is the code snippet(swift 5) for it我试图有一个视图,其中 3 个点可以为无穷大设置动画,这是它的代码片段(swift 5)

func showAnimatingDotsInImageView() {
        let lay = CAReplicatorLayer()
        lay.frame = CGRect(x: 0, y: 0, width: 15, height: 7) //yPos == 12
        let circle = CALayer()
        circle.frame = CGRect(x: 0, y: 0, width: 7, height: 7)
        circle.cornerRadius = circle.frame.width / 2
        circle.backgroundColor = UIColor.green.cgColor
        lay.addSublayer(circle)
        lay.instanceCount = 3
        lay.instanceTransform = CATransform3DMakeTranslation(10, 0, 0)
        let anim = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
        anim.fromValue = 1.0
        anim.toValue = 0.2
        anim.duration = 1
        anim.repeatCount = .infinity
        circle.add(anim, forKey: nil)
        lay.instanceDelay = anim.duration / Double(lay.instanceCount)
        test1.layer.addSublayer(lay)
    }

In the above code test1 is a UIImageView.在上面的代码中 test1 是一个 UIImageView。 The main issue is the dots are shown but it's static and doesn't animate.主要问题是显示了点,但它是静态的并且没有动画。

I tried your code and it is animating just fine.我试过你的代码,它的动画效果很好。 Any chance you are calling this from a thread which is different than the Main thread?您是否有机会从与主线程不同的线程调用它? Like for example something on the background is happening, which you want to triggers the loading?例如,背景中的某些事情正在发生,您想触发加载?

Try adding:尝试添加:

DispatchQueue.main.async { [weak self] in
    self?.showAnimatingDotsInImageView()
}

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

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