iOS中是否有任何功能可以暂停UIImageView动画?

时间:2017-08-23 12:03:33

标签: ios swift uiimageview

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

3 个答案:

答案 0 :(得分:2)

没有。 您无法暂停动画阅读此文档 UIImageView - Animation

UIImageView没有任何处理pauseAnimation操作的函数/属性成员。

enter image description here

答案 1 :(得分:0)

您可以通过恢复和暂停图层

来使用它
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
}

答案 2 :(得分:0)

仅举例来说

NSArray *myAnimationFrames = [NSArray arrayWithObjects:
  [UIImage imageWithName:@"myImageFirst.png"],
  [UIImage imageWithName:@"myImageSecond.png"], 
  [UIImage imageWithName:@"myImageThird.png"], 

  nil];
//Create imageview object
UIImageView *animatedImageView = [[UIImageView alloc] init];
animatedImageView.animationImages = myAnimationFrames;
[animatedImageView setAnimationRepeatCount:1];
[animatedImageView startAnimating];

Swift版

  let arrayImages = [UIImage(named: "myImageFirst.png"), UIImage(named: "myImageSecond.png"),UIImage(named: "myImageThird.png")]

    let imageView = UIImageView()
    imageView.animationImages = arrayImages as? [UIImage]
    imageView.animationRepeatCount = 1
    imageView.startAnimating()

    //for stopping 
    imageView.stopAnimating()