2

I have an animate function which contains CATransaction.begin() I want this animation to be repeated infinitely or for a defined number of times. How do I make that happen?

This is the animate function if you need to see the code:

 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()
    }
w.y
  • 99
  • 2
  • 11

1 Answers1

1

I think CATransaction redundant there

If I understand what you want to achieve

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)

EDIT: Recursive function to do pulse in circles

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)
    }
}
MichaelV
  • 1,231
  • 8
  • 10
  • My program creates several views, arranges them in a circular path, and then each view animates "transform" in an ordered manner, in other words, one by one. Your piece of code made all of the views animate together and at once. What I'm trying to do is repeat this animation cycle because right now, it only occurs once. – w.y Mar 05 '18 at 16:52
  • You want them all pulsing together after first round. Out you want them pulsing in circles? – MichaelV Mar 05 '18 at 17:11
  • I want them to pulse one by one, not together and I want this cycle to be repeated continuously. – w.y Mar 05 '18 at 17:14
  • .repeat makes the single view repeat the pulsing continuously and I dont want that because as I've mentioned, I'm trying to repeat the entire cycle. – w.y Mar 05 '18 at 17:17
  • It's unrational to do in one go, much better to do in recursive function – MichaelV Mar 05 '18 at 17:24
  • The recursive function did not work. I get the Thread1: EXC_BAD_ACCESS (code=2, address=0x16fcbffb0) error after running – w.y Mar 07 '18 at 17:07
  • My mistake - you had to use `let view = views[resolvedIndex]` PS: I advice you to understand proposed solution rather then copy/past it. – MichaelV Mar 08 '18 at 10:47