0

An oldish retired DEC PDP 8 assembly programmer needs another set of eyes. I created a Hello World program that displays images of the family. The animation works fine, but I would like the user to be able to stop the animation so I added a button to “Pause” the image so that the user can view it at their leisure. However, the .stopAnimation function clears the view. How can this be prevented. Please be kind.

import UIKit

class FamilyUnitController: UIViewController {
    @IBOutlet weak var familyMember: UILabel!
    @IBOutlet weak var familyPhotos: UIImageView!
    var familyName: String = ""
    var familyUnitArray = [String]()

    @IBOutlet weak var pauseButton: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
enter code here
        configureImageView()
    }

    @IBAction func pauseImage(_ sender: UIButton) {
        if familyPhotos.isAnimating {
            familyPhotos.stopAnimating()
            pauseButton.setTitle("Paused", for: [])
        } else {
            familyPhotos.startAnimating()
            pauseButton.setTitle("Pause", for: [])
        }
    }

    func configureImageView() {

        familyMember.text = familyUnitArray[0]
        familyMember.sizeToFit()
        let imagePrefix = familyUnitArray[1]
        print ("configureImageView called...")
        let maxImageCnt = 10
        if let imageView = familyPhotos {
            print ("Processing images...")
            let fileManager = FileManager.default
            let path = Bundle.main.resourcePath!
            print (#function,">Path> ",path)
            let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: path)!
            var imageArr = [UIImage]()
            var imageCnt: Int = 0
            while let element = enumerator.nextObject() as? String {
                print (#function,"> element.description",element.debugDescription)
                let tmpStr = element.lowercased()
                let tmpImagePrefix = imagePrefix.lowercased()
                if (element.hasSuffix("png") || element.hasSuffix("jpg") || element.hasSuffix("jpeg"))
                        && tmpStr.hasPrefix(tmpImagePrefix)
                        && imageCnt < maxImageCnt {
                    let elementWithPath = path+"/"+element
                    imageArr.append(UIImage(contentsOfFile: elementWithPath)!)
                    imageCnt += 1
                }
            }
            imageView.isUserInteractionEnabled = true
            imageView.animationImages = imageArr
            imageView.contentMode = .scaleAspectFit
            imageView.backgroundColor = UIColor.white
            imageView.animationDuration = 15
            imageView.startAnimating()
        }
    }
}
Jazzfess
  • 1
  • 2

1 Answers1

0

You may refer here to for pausing the animation / not stopping it.

Is there a way to pause a CABasicAnimation?

In your case ,the code should be like the following, you may need to change a little to make it perfect.

@IBAction func pauseImage(_ sender: UIButton) {
    if  familyPhotos.layer.speed != 0 {

    let time   =  familyPhotos.layer.convertTime(CACurrentMediaTime(), to: nil)

        familyPhotos.layer.speed = 0
        familyPhotos.layer.timeOffset  = time
        pauseButton.setTitle("Paused", for: [])
    } else {

       familyPhotos.layer.beginTime =  familyPhotos.layer.timeOffset - CACurrentMediaTime()
        familyPhotos.layer.timeOffset = 0
        familyPhotos.layer.speed = 1
        pauseButton.setTitle("Pause", for: [])
    }
}
E.Coms
  • 11,065
  • 2
  • 23
  • 35