Home > other >  How to sort array of UIImages?
How to sort array of UIImages?

Time:09-24

I have 3 images, I need to implement them with animation. How i can display each image separately on the screen using a loop? help me please

class ViewController: UIViewController {
    var imageArray: [UIImage] = [UIImage(named: "1")!, UIImage(named: "2")!, UIImage(named: "3")!]
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(handleTimer), userInfo: nil, repeats: false)
    }
    
    private func setImageSettings(to image: UIImage?) -> UIImageView {
        let pictureParams = UIImageView(image: image)
        pictureParams.frame.size = CGSize(width: 400, height: 300)
        pictureParams.center = view.center
        pictureParams.contentMode = .scaleAspectFit
        view.addSubview(pictureParams)
        return pictureParams
    } 
    func moveImages(_ targetView: UIImageView) {
        UIImageView.animate(withDuration: 2, delay: 0, options: [], animations: {targetView.frame.origin = CGPoint(x: targetView.frame.origin.x   320, y: targetView.frame.origin.y )}, completion: nil)
    }   
    func sortImages() {
        for image in imageArray{
            moveImages(setImageSettings(to: image))
        }
    }
    @objc private func handleTimer(_ sender: Timer) {
        sortImages()
    }
}

CodePudding user response:

Assuming you want to animate one image at a time, appearing in the middle of your view controller's view, and then moving up to the top of the view, you could change your code like this:

class ViewController: UIViewController {
    var imageIndex = 0
    var imageArray: [UIImage] = [UIImage(named: "1")!, UIImage(named: "2")!, UIImage(named: "3")!]
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        let timer: Timer? = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(handleTimer), userInfo: nil, repeats: true)
    }
    
    private func setImageSettings(to image: UIImage?) -> UIImageView {
        let pictureParams = UIImageView(image: image)
        pictureParams.frame.size = CGSize(width: 400, height: 300)
        pictureParams.center = view.center
        pictureParams.contentMode = .scaleAspectFit
        view.addSubview(pictureParams)
        return pictureParams
    } 

    func moveImages(_ targetView: UIImageView) {
        UIImageView.animate(withDuration: 2, delay: 0, options: [], animations: {targetView.frame.origin = CGPoint(x: targetView.frame.origin.x   320, y: targetView.frame.origin.y )}, completion: nil)
    }   

    func animateImages() {
        // Fetch an image from the array
        let image = imageArray[imageIndex]

        // Create the animation for that image
        moveImages(setImageSettings(to: image))

        // Move on to the next image index
        imageIndex  = 1

        // If we are past the end of the array, stop.
        if imageIndex == imagesArray.count {
            timer.invalidate() 
        }
    }

    @objc private func handleTimer(_ sender: Timer) {
        animateImages()
    }
}
  • Related