如何让CATransaction无限重复? - 斯威夫特

问题描述 投票:2回答:1

我有一个包含CATransaction.begin()的动画功能我希望这个动画无限重复或定义次数。我该如何做到这一点?

如果您需要查看代码,这是动画功能:

 private func animate(views: [UIView], duration: TimeInterval, intervalDelay: TimeInterval) {

        CATransaction.begin()
        CATransaction.setCompletionBlock {
            print("COMPLETED ALL ANIMATIONS")
        }

        var delay: TimeInterval = 0.0
        let interval = duration / TimeInterval(views.count)

        for view in views {
            let transform = view.transform

            UIView.animate(withDuration: interval, delay: delay, options: [.curveEaseIn], animations: {

                view.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)

            }, completion: { (finished) in

                UIView.animate(withDuration: interval, delay: 0.0, options: [.curveEaseIn], animations: {

                    view.transform = transform

                }, completion: { (finished) in


                })
            })

            delay += (interval * 2.0) + intervalDelay
        }
        CATransaction.commit()
    }
ios swift catransaction
1个回答
1
投票

我认为CATransaction在那里多余

如果我明白你想要实现的目标

UIView.animate(withDuration: interval, delay: delay, options: [.curveEaseIn, .autoreverse, .repeat], animations: { 
    self.views.forEach{$0.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)}
}, completion: nil)

编辑:递归函数在圆圈中做脉冲

func pulse(index: Int) {
    guard views.count > 0 else { return }
    let resolvedIndex = (views.count < index) ? index : 0
    let duration = 1.0
    let view = views[resolvedIndex]
    UIView.animate(withDuration: duration, delay: 0, options: [.curveEaseIn,.autoreverse], animations: {
        self.view.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
    }) { [weak self] _ in
        self?.pulse(index: resolvedIndex + 1)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.