2

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

如果您需要查看代码,这是 animate 函数:

 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()
    }
4

1 回答 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)
    }
}
于 2018-03-05T16:11:02.567 回答