UIImageView未设置动画



我正在尝试一个视图,其中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)
}

在上面的代码中,test1是一个UIImageView。主要问题是显示了点,但它是静态的,没有动画。

我试过你的代码,它的动画效果很好。你有没有可能从一个不同于主线程的线程调用这个?例如,后台发生了什么事情,你想触发加载吗?

尝试添加:

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

最新更新