In this tutorial apply Pan Gesture for Animation.

Make UI for DragToDelete Project:

Create a new Xcode Project

Put simple two UIImageView at screen one for file image and the second one is cross image and makes a connection with ViewController.swift  file using IBOutlet. Put one Reset UIButton and makes a connection with ViewController.swift file using IBAction.

@IBOutlet weak var imgDelete: UIImageView!
@IBOutlet weak var imgFile: UIImageView!

When click Reset button at a time create new imageView and append in to arrAddView array. Add Pangesture in imageView.

@IBAction func btnResetTap(_ sender: Any)
        	        imgDelete.isHidden = true
        let imageName = "file"
        let image = UIImage(named: imageName)
        let imageView = UIImageView(image: image!)
        imageView.frame = CGRect(x: 16, y: 20, width: 80, height: 80)
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(recognizer:)))
        imageView.isUserInteractionEnabled = true

Add Pan gesture in file-imageview and make a connection with ViewController.swift  file using IBOutlet.

@IBOutlet var objPan: UIPanGestureRecognizer! 

Inherit UIGestureRecognizerDelegate superclass with a viewcontroller:

	class ViewController: UIViewController,UIGestureRecognizerDelegate

Create a global variable for screen height and width. And create a global Array to store created image view.

var arrAddView = Array()
    let screenHeight: CGFloat = UIScreen.main.bounds.size.height
    let screeWidth: CGFloat = UIScreen.main.bounds.size.width

Cross imageView by default hide.

override func viewDidLoad()

        objPan.delegate = self
        imgDelete.isHidden = true

Action selector method name handlePan() is used to handle imageView movement and perform some actions on view or any functionality made in app.

In handlePan() method write the code as given below:

@IBAction func handlePan(recognizer: UIPanGestureRecognizer)
        let translation = recognizer.translation(in: self.view)
        if recognizer.state == UIGestureRecognizerState.began
            imgDelete.isHidden = false
        if recognizer.state == UIGestureRecognizerState.ended
            imgDelete.isHidden = true

        if let view = recognizer.view
   = CGPoint( + translation.x, +   translation.y)
            if (( <= (screenHeight - 60)) && ( >= (screenHeight - 140)) && ( >= (screeWidth/2 - 50)) && ( <= (screeWidth/2 + 50)))
                var i = 0
                while i < self.arrAddView.count
                    (self.arrAddView[i] as AnyObject).removeFromSuperview()
                    i += 1
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.3)
                    self.imgDelete.isHidden = true
        recognizer.setTranslation(, in: self.view)

Now, build the App (cmd + b) & Run (cmd + r) and finally you can test your project at a time on the screen.

You can easily download demo code by clicking here.

Contact us; If you have any query regarding iOS Application / Apple Watch Application / iMessage Extension / Today’s Extension OR you have your own application idea let us know. We have expert iOS team for your help.