Gestures
If we want to recognize the user's touch events we have to use gesture recognizers. Events like pan, swipe, pinch ... are recognizable view UIGestureRecognizer class. We need to do the following steps for implementing Gestures.
1. Add the gesture recognizer to the view in order to recognize the gesture
2. Provide an implementation to handle the gesture
There are two approach in order to implement the above steps. First through the storyboard, second programatically. Lets start with the storyboard:
Add GestureRecognizer using Storyboard
1. From right bar side of Xcode in Object pallet, drag your desired gesture and drop it on the view that you want to implement the gesture.2. Once you are done with the previous step, an icon will appear at the top of the view controller. If you go hover it, it will show the type of the gesture (e.g Swipe Gesture Recognizer). Control drag from this icon to your controller and make an IBAction. You can have your implementation inside of that method
Add GestureRecognizer programmatically
1. Create an outlet for the view that we want to have gesture for it, inside of it's controller. Control drag from your custom view to it's controller. If your view is created programmatically you don't need to create an outlet, just use the name of your view for next step.2. Add the desired gesture recognizer to the outlet.
[self.yourCustomView addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self.yourCustomView action:@selector(pan:)]];3. Implement or handle the gesture inside of custom view class (not controller).
No comments:
Post a Comment