[Solved] how to make a sliding door animation in Xcode


I am breaking the rules by giving up the answer when it appears you have not googled enough. The general rule is to show the code you have attempted and indicate what part you are having issues with.

The code below will achieve what you are attempting to do.

import UIKit

class slidingIamgesViewController: UIViewController {

    @IBOutlet weak var topImage: UIImageView!
    @IBOutlet weak var bottomImage: UIImageView!
    var doubleTap: Bool! = false

    //MARK: View LifeCycle
    override func viewDidLoad() {
        super.viewDidLoad()
        let singleFingerTap = UITapGestureRecognizer(target: self, action: #selector(slidingIamgesViewController.handleSingleTap(_:)))
        self.view.addGestureRecognizer(singleFingerTap)
    }


    // MARK: gestutre recognizer
    func handleSingleTap(_ recognizer: UITapGestureRecognizer) {
        if (doubleTap) {
        UIView.animate(withDuration: 0.7, delay: 1.0, options: .curveEaseOut, animations: {
            var basketTopFrame = self.topImage.frame
            basketTopFrame.origin.y += basketTopFrame.size.height

            var basketBottomFrame = self.bottomImage.frame
            basketBottomFrame.origin.y -= basketBottomFrame.size.height

            self.topImage.frame = basketTopFrame
            self.bottomImage.frame = basketBottomFrame
        }, completion: { finished in
            print("Images Moved back!")
        })
            doubleTap = false
        } else {
            UIView.animate(withDuration: 0.7, delay: 1.0, options: .curveEaseOut, animations: {
                var basketTopFrame = self.topImage.frame
                basketTopFrame.origin.y -= basketTopFrame.size.height

                var basketBottomFrame = self.bottomImage.frame
                basketBottomFrame.origin.y += basketBottomFrame.size.height

                self.topImage.frame = basketTopFrame
                self.bottomImage.frame = basketBottomFrame
            }, completion: { finished in
                print("Images sperated!")
            })
            doubleTap = true
        }
    }

}

Make sure in your storyboard to added a Tap Gesture Recognizer.

2

solved how to make a sliding door animation in Xcode