簡體   English   中英

UIImageView 沒有動畫

[英]UIImageView is not animating

我試圖有一個視圖,其中 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()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM