Gesture Recognition in iOS development

Source: Internet
Author: User

Gesture Recognition in iOS development
I feel it is necessary to make a small Summary of Gesture Recognition in iOS development. In the previous iOS-developed custom emoticons keyboard (Component encapsulation and automatic layout) blog, we used a light-click gesture, that is, returning the emoticons keyboard to the system keyboard when you tap TextView, the hand in TextView is added with storyboard. Next we will first show how to use storyboard to add a gesture to the corresponding control, and then add a gesture to our control in code-only mode. The gesture usage is relatively simple. Similar to the use of the button, it is also the callback of the target action. If you don't talk much about it, let's start with today's topic. There are six types of Gesture Recognition: TapGestureRecognizer, LongPressGestureRecognizer, and PanGestureRecognizer ), rotationGestureRecognizer; in fact, these gestures can be fully implemented using touche events. Apple encapsulates common touch events into gestures for users. Readers can use TouchesMoved to write and drag gestures. storyboard is used to add gesture recognition to the control. Of course, storyboard is used to cut a picture. using storyboard to add gesture recognition is the same as adding a Button. First, we need to find the corresponding gesture and drag the gesture recognition control to the control we want to add the gesture, as shown below: 2. add a callback event to the gesture we dragged out, which is no different from the callback event for the Button. Add the business logic to be implemented in the callback method, adding Gesture Recognition with pure code with storyboard can greatly simplify our operations, but the pure code method is still needed, just like Dreamwear editing web pages (of course, storyboard's drag-and-drop function is much more powerful than Dreamwear's drag-and-drop function). It is more flexible and easier to maintain with pure code. However, using storyboard can reduce our workload. These two must be used together to greatly improve our development efficiency. I personally feel that it is better to use storyboard to build the framework (the relationship between controllers. The following describes how to add Gesture Recognition to our controls in pure code mode. 1. the code for the initialization code TapGestureRecongnizer added by the click gesture (TapGestureRecongnizer) is as follows: Copy code 1 // create a tap gesture 2 ← * tapGesture = [[adjust alloc] initWithTarget: self action: @ selector (tapGesture :)]; 3 // you can specify the number of clicks and the number of clicks. 4. tapGesture. numberOfTapsRequired = 1; // Number of clicks 5 tapGesture. numberOfTouchesRequired = 1; // click the hand index 6 [self. view addGestureRecognizer: tapGesture]; copy the code and add the corresponding business logic to the callback method: 1 // method 2 for triggering a light-click gesture (void) TapGesture :( id) sender3 {4 // what to do after a light attack 5} 2. longPressGestureRecognizer initialization code: Copy code 1 // Add long-click gesture 2 ← * longPressGesture = [[langualloc] initWithTarget: self action: @ selector (longPressGesture :)]; 3 // set the duration 4 longPressGesture. minimumPressDuration = 0.5; // (2 seconds) 5 [self. view addGestureRecognizer: longPressGesture]; copy the code and add the corresponding method to the corresponding callback method (executed when the gesture starts): Copy code 1/ /Method 2-(void) longPressGesture: (id) sender 3 {4 UILongPressGestureRecognizer * longPress = sender; 5 if (longPress. state = UIGestureRecognizerStateBegan) 6 {7 UIAlertView * alter = [[UIAlertView alloc] initWithTitle: @ "prompt" message: @ "Long press trigger" delegate: nil cancelButtonTitle: @ "cancel" otherButtonTitles: nil]; 8 [alter show]; 9} 10} copy the code. Code Description: The common status of the gesture is as follows: UIGestureRecognizerStateBegan changed: UIGestureRecogni ZerStateChanged ended: UIGestureRecognizerStateEnded canceled: failed: fail 3. Specify the direction of the swipe when initializing the swipe gesture (SwipeGestureRecognizer. If you want to add multiple light scanning directions, you need to add multiple light scanning gestures, but the same method is called back. Add a swipe gesture, one to the right from the left, the Code is as follows: Copy code 1 // Add the swipe gesture 2 UISwipeGestureRecognizer * swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget: @ selector (swipeGesture :)]; 3 // sets the direction of the Light sweep 4 swipeGesture. direction = UISwipeGestureRecognizerDirectionRight; // default value: 5 to the right [self. view addGestureRecognizer: swipeGesture]; 6 7 // Add a swipe gesture 8 UISwipeGestureRecognizer * swipeGestureLeft = [[UISwipeGestureRecognizer alloc] in ItWithTarget: self action: @ selector (swipeGesture :)]; 9 // set the direction of the Light sweep to 10 swipeGestureLeft. direction = uiswipegesturerecognizerdireleft; // The default value is 11 to the right [self. view addGestureRecognizer: swipeGestureLeft]; the callback Method for copying code is as follows: Copy code 1 // Method for triggering a swipe gesture 2-(void) swipeGesture :( id) sender 3 {4 UISwipeGestureRecognizer * swipe = sender; 5 if (swipe. direction = uiswipegesturerecognizerdireleft) 6 {7 // sweep to the left to do 8} 9 if (swip E. direction = uiswipegesturerecognizerdireright) 10 {11 // do things to the right 12} 13} 14 copy code 4. pinchGestureRecognizer. view addGestureRecognizer: pinchGesture) sender 3 {4 UIP InchGestureRecognizer * gesture = sender; 5 6 // 7 if (gesture. state = UIGestureRecognizerStateChanged) 8 {9 // zoom ratio of the scale attribute record in the kneading gesture: 10 _ imageView. transform = CGAffineTransformMakeScale (gesture. scale, gesture. scale); 11} 12 13 // restore 14 if (gesture. state = UIGestureRecognizerStateEnded) 15 {16 [UIView animateWithDuration: 0.5 animations: ^ {17 _ imageView. transform = CGAffineTransformIdentity; // cancel all deformation 1 8}]; 19} 20} copy Code 5. drag gesture initialization 1 // Add drag gesture 2 ← * panGesture = [UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panGesture :)]; 3 [self. view addGestureRecognizer: panGesture]; copy code 1 // drag gesture 2-(void) panGesture :( id) sender3 {4 UIPanGestureRecognizer * panGesture = sender; 5 6 CGPoint movePo Int = [panGesture translationInView: self. view]; 7 8 // do what you want to do 9} copy Code 6. rotationGestureRecognizer rotation gesture initialization 1 // Add rotation gesture 2 rotate * rotationGesture = [[rotate alloc] initWithTarget: self action: @ selector (rotationGesture :)]; 3 [self. view addGestureRecognizer: rotationGesture]; method of call for rotation gesture: 1 // rotation gesture 2-(void) rotationGesture :( id) sender 3 {4 5 UIRotationGestureReco Gnizer * gesture = sender; 6 7 if (gesture. state = UIGestureRecognizerStateChanged) 8 {9 _ imageView. transform = CGAffineTransformMakeRotation (gesture. rotation); 10} 11 12 if (gesture. state = UIGestureRecognizerStateEnded) 13 {14 15 [UIView animateWithDuration: 1 animations: ^ {16 _ imageView. transform = CGAffineTransformIdentity; // cancel deformation 17}]; 18} 19 20} There is not much advanced technology on the above, that is, a small summary of the gestures in iOS development, let's take a look at the basic knowledge. In the previous blog, the content of gesture recognition is also useful, that is, there is no system to sort out the knowledge of Gesture Recognition. This blog is a basic supplement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.