UITapGestureRecognizer Tutorial in Swift 3.0

A Gesture Recognizing is one type of touch behaviour on screen of user. By use of that we can finding which type of event on screen. user can do on screen like single tap , double tap , rotate his/her finger on screen , Drag finger , Pinch with finger , Swipe etc. 

There are Six types of Gesture Recognises :

 UITapGestureRecognizer
 UIPinchGestureRecognizer
  UIRotationGestureRecognizer
 UISwipeGestureRecognizer
 UIPanGestureRecognizer
 UILongPressGestureRecognizer




This is tutorial only for UITapGestureRecognizer.

Follow the below steps :

Step 1 :  Create Project with File > New > Project > TapGesture-Swift3.

Step 2 :  Adding a simple View on Storyboard. and also give IBOutlet connection of the view.
1@IBOutlet var viewTap: UIView!
Step 3 :  Make an instance of the UITapGestureRecognizer.
1var tapGesture = UITapGestureRecognizer()
Step 4 :  Initialise the UITapGestureRecognizer and adding tap gesture in view. So add the code in ViewDidLoad.
123456789override func viewDidLoad() {
    super.viewDidLoad()
    // TAP Gesture
    tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.myviewTapped(_:)))
    tapGesture.numberOfTapsRequired = 1
    tapGesture.numberOfTouchesRequired = 1
    viewTap.addGestureRecognizer(tapGesture)
    viewTap.isUserInteractionEnabled = true
}


Step 5 :  Method of the TapGesture.
12345678func myviewTapped(_ sender: UITapGestureRecognizer) {

    if self.viewTap.backgroundColor == UIColor.yellow {
        self.viewTap.backgroundColor = UIColor.green
    }else{
        self.viewTap.backgroundColor = UIColor.yellow
    }
}


Step 6 :  Output.



Step 7 : 
 Download UITapGestureRecognizer Demo

Thanks.

UITapGestureRecognizer Tutorial in Swift 3.0 UITapGestureRecognizer Tutorial in Swift 3.0 Reviewed by KIRIT MODI on 06:55:00 Rating: 5
Powered by Blogger.