shashank
shashank

Reputation: 33

Is there any function in iOS to pause UIImageView animation?

Is there any function in iOS to pause UIImageView animation like startAnimating() and stopAnimating() functions are there to start and stop an animation respectively?

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

Upvotes: 0

Views: 808

Answers (3)

Abdelahad Darwish
Abdelahad Darwish

Reputation: 6067

you can use this by resume and pause layer

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
}

Upvotes: 2

Gagan_iOS
Gagan_iOS

Reputation: 4060

Just for example

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 version

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

Upvotes: -1

Divyesh Gondaliya
Divyesh Gondaliya

Reputation: 904

No. You can not pause animation read this document UIImageView - Animation

UIImageView has not any function/property member that handle pauseAnimation operation.

enter image description here

Upvotes: 0

Related Questions