0

I have this animation:

override func viewDidLoad() {
        super.viewDidLoad()
_ = Timer.scheduledTimer(timeInterval: 15, target: self, selector: #selector(self.callingFunction), userInfo: nil, repeats: true)
}

@objc func callingFunction(){
        let image0:UIImage = UIImage(named: "next0.png")!
        let image1:UIImage = UIImage(named: "next1.png")!
        let image2:UIImage = UIImage(named: "next2.png")!
        let image3:UIImage = UIImage(named: "next3.png")!
        let image4:UIImage = UIImage(named: "next4.png")!
        let image5:UIImage = UIImage(named: "next5.png")!
        let image6:UIImage = UIImage(named: "next6.png")!
        let image7:UIImage = UIImage(named: "next7.png")!

        nextButton.setImage(image0, for: .normal)
        nextButton.imageView!.animationImages = [image0, image1, image2, image3, image4, image5, image6, image7]
        nextButton.imageView!.animationDuration = 1.0
        nextButton.imageView!.startAnimating()
    }

I need to repeat this animation in every 15 seconds. But this animation starts after 15 seconds and next execute every 1 second.

user
  • 83
  • 12

3 Answers3

1

You have to call function by using timer. Set timeInterval to 15 seconds.

callingFunction()
timer = Timer.scheduledTimer(timeInterval: 15, target: self, selector: #selector(self.callingFunction), userInfo: nil, repeats: true)

func callingFunction(){
      //Execute your code
      let image0:UIImage = UIImage(named: "next0.png")!
let image1:UIImage = UIImage(named: "next1.png")!
let image2:UIImage = UIImage(named: "next2.png")!
let image3:UIImage = UIImage(named: "next3.png")!
let image4:UIImage = UIImage(named: "next4.png")!
let image5:UIImage = UIImage(named: "next5.png")!
let image6:UIImage = UIImage(named: "next6.png")!
let image7:UIImage = UIImage(named: "next7.png")!

nextButton.setImage(image0, for: .normal)
nextButton.imageView!.animationImages = [image0, image1, image2, image3, image4, image5, image6, image7]
nextButton.imageView!.animationDuration = 1.0
nextButton.imageView!.animationRepeatCount = 1
nextButton.imageView!.startAnimating()
}
Pranavan SP
  • 1,785
  • 1
  • 17
  • 33
1

Just Updating your code

Just you need to set animationRepeatCount = 1 (this will help image change will stop in one), and when a timer reaches to 15sec then again call your callingFunction.

override func viewDidLoad() {
    super.viewDidLoad()
    /// You need to take class object here for timer, when viewWillDisapper called you need to invalidate timer other wise you will fase memory issues
    self.timer = Timer.scheduledTimer(timeInterval: 15, target: self, selector: #selector(self.timerTimeUp), userInfo: nil, repeats: true)
}

@objc func timerTimeUp() {
    self.callingFunction()
}

func callingFunction(){
    let image0:UIImage = UIImage(named: "next0.png")!
    let image1:UIImage = UIImage(named: "next1.png")!
    let image2:UIImage = UIImage(named: "next2.png")!
    let image3:UIImage = UIImage(named: "next3.png")!
    let image4:UIImage = UIImage(named: "next4.png")!
    let image5:UIImage = UIImage(named: "next5.png")!
    let image6:UIImage = UIImage(named: "next6.png")!
    let image7:UIImage = UIImage(named: "next7.png")!

    nextButton.setImage(image0, for: .normal)
    nextButton.imageView!.animationImages = [image0, image1, image2, image3, image4, image5, image6, image7]
    nextButton.imageView!.animationDuration = 1.0
    nextButton.imageView!.animationRepeatCount = 1
    nextButton.imageView!.startAnimating()
}

Updated Code

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.invalidateTimer()
}

func invalidateTimer() {
   if self.timer != nil {
      self.timer.invalidate()
      self.timer = nil
   }
}

You can call invalidateTimer() method, whenever view dismiss/Disappear.

Hope this will help you.

PiyushRathi
  • 956
  • 7
  • 21
0

You can use a timer:

let timer = Timer.scheduledTimer(withTimeInterval: 15, repeats: true) {
     [weak self] (timer) in
     self?.nextButton.imageView?.startAnimating()
}

To stop it:

timer.invalidate()
ukim
  • 2,395
  • 15
  • 22