我有这个动画:
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()
}
我需要每15秒重复一次此动画。但是此动画将在15秒后开始,然后每1秒执行一次。
答案 0 :(得分:1)
您必须使用计时器来调用函数。将timeInterval设置为15秒。
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()
}
答案 1 :(得分:1)
仅更新代码
只需设置animationRepeatCount = 1(这将有助于图像更改停止一次),然后当计时器达到15秒时,再次调用您的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()
}
更新代码
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.invalidateTimer()
}
func invalidateTimer() {
if self.timer != nil {
self.timer.invalidate()
self.timer = nil
}
}
每当视图关闭/消失时,您都可以调用invalidateTimer()
方法。
希望这会对您有所帮助。
答案 2 :(得分:0)
您可以使用计时器:
let timer = Timer.scheduledTimer(withTimeInterval: 15, repeats: true) {
[weak self] (timer) in
self?.nextButton.imageView?.startAnimating()
}
要停止它:
timer.invalidate()