gpt4 book ai didi

ios - iOS 有暂停 UIImageView 动画的功能吗?

转载 作者:行者123 更新时间:2023-11-28 12:17:55 34 4
gpt4 key购买 nike

iOS 中是否有暂停 UIImageView 动画的功能,例如 startAnimating() 和 stopAnimating() 函数分别用于启动和停止动画?

let imageView: UIImageView = UIImageView(image: "Chicken"))
let playButton: UIButton = UIButton(type: .system)
let stopButton: UIButton = UIButton(type: .system)

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = UIColor.cyan

let imagesArray = [UIImage(named: "Chicken"), UIImage(named: "Burger"), UIImage(named: "Fruits"), UIImage(named: "Pizza"), UIImage(named: "Sandwich")]

imageView.frame = CGRect(x: 80, y: 80, width: 200, height: 200)
view.addSubview(imageView)
imageView.animationImages = imagesArray as? [UIImage]
imageView.animationDuration = 2
imageView.animationRepeatCount = 4

playButton.frame = CGRect(x: 80, y: 320, width: 40, height: 40)
playButton.setTitle("play", for: .normal)
playButton.setTitleColor(UIColor.black, for: .normal)
playButton.backgroundColor = UIColor.green
playButton.addTarget(self, action: #selector(playButtonAction), for: .touchUpInside)
view.addSubview(playButton)

stopButton.frame = CGRect(x: 140, y: 320, width: 40, height: 40)
stopButton.setTitle("stop", for: .normal)
stopButton.setTitleColor(UIColor.black, for: .normal)
stopButton.backgroundColor = UIColor.green
stopButton.addTarget(self, action: #selector(stopButtonAction), for: .touchUpInside)
view.addSubview(stopButton)
}

func playButtonAction(sender: UIButton){
imageView.startAnimating()
}

func stopButtonAction(sender: UIButton){
imageView.stopAnimating()
}

最佳答案

你可以通过恢复和暂停层使用它

override func viewDidLoad() {
super.viewDidLoad()


let images = [UIImage]()
let imageView = UIImageView()
imageView.animationImages = images //images is your array
imageView.startAnimating()

self.pauseLayer(layer: imageView.layer)

}

func pauseLayer(layer: CALayer) {
let pausedTime = layer.convertTime(CACurrentMediaTime(), from: nil)
layer.speed = 0.0
layer.timeOffset = pausedTime
}

func resumeLayer(layer: CALayer) {
let pausedTime = layer.timeOffset
layer.speed = 1.0
layer.timeOffset = 0.0
layer.beginTime = 0.0
let timeSincePause = layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
layer.beginTime = timeSincePause
}

关于ios - iOS 有暂停 UIImageView 动画的功能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45839205/

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