gpt4 book ai didi

ios - swift:一段时间后重复动画

转载 作者:搜寻专家 更新时间:2023-11-01 06:58:52 25 4
gpt4 key购买 nike

我有这个动画:

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 秒执行一次。

最佳答案

只是更新你的代码

只需要设置 animationRepeatCount = 1(这将有助于图像更改一次停止),当计时器达到 15 秒时再次调用您的调用函数。

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
}
}

每当 View 关闭/消失时,您都可以调用 invalidateTimer() 方法。

希望对您有所帮助。

关于ios - swift:一段时间后重复动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51759185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com