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 :
UITap[1]GestureRecognizer[2]
UIPinch Gesture Recognizer[3]
UIRotationGestureRecognizer[4]
UISwipe Gesture Recognizer
UIPan Gesture Recognizer
UILong Press Gesture Recognizer
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.
@IBOutlet var viewTap: UIView!
Step 3 : Make an instance of the UITapGestureRecognizer.
var tapGesture = UITapGestureRecognizer()
Step 4 : Initialise the UITapGestureRecognizer and adding tap gesture in view. So add the code in ViewDidLoad. override 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.
func 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[5] Demo.
Thanks.
References
- ^ UITap (github.com)
- ^ GestureRecognizer (github.com)
- ^ Gesture Recognizer (www.blogger.com)
- ^ UIRotationGestureRecognizer (iosdevcenters.blogspot.com)
- ^ UITapGestureRecognizer (github.com)