The use of touch events for iOS and the difference from gesture recognizers

Source: Internet
Author: User

1, first to introduce the advantages and disadvantages of touch event and gesture recognizer

Touch event and gesture recognizer there's a direct relationship between the two. Gesture recognizers evolved on the basis of touch events when we use the touch event, the default UIView is the  only way to customize the view to implement event touch is usually as follows: – Touchesbegan:withevent:–touchesmoved:withevent:–touchesended:withevent:-Touchescancelled:withevent: And the gesture recognizer is encapsulated on the basis of touch events. Because the direct Touchs method to locate gestures is more troublesome. In the era of not Uigesturerecognizer, with Touchs can also calculate the blog post said several commonly used gestures: point, slide, drag and so on. These gestures are very common, and each developer wants to monitor the gestures to make their own judgments, and everyone is making the wheel again. The Uigesturerecognizer and the corresponding sub-class gestures are available when the gestures are standardized. Then why should there be touchs and other methods exist? Because Uigesturerecognizer several gestures are more limited, some game applications need to make some special gestures, then use touchs these methods to define.

2. Use gesture steps

Using gestures is simple and two-step: Create a gesture instance. When creating gestures, specify a callback method that is called when the gesture starts, changes, or ends. Added to the view that needs to be recognized. Each gesture corresponds to only one view, and when the screen touches within the bounds of the view, if the gesture is the same as the reservation, the callback method is used. PS: A gesture can only correspond to one view, but a view can have multiple gestures. It is recommended to run these gestures on the real machine, which is inconvenient for the simulator and may cause you to think that the gesture is invalid.

3, gesture of introduction

UITapGestureRecognizer (  percussion) uipinchgesturerecognizer (pinch, due to scaling) Uipangesturerecognizer   (drag) Uiswipegesturerecognizer (swipe) uirotationgesturerecognizer (rotate) Uilongpressgesturerecognizer (Long Press)

4, the use of percussion gestures (other gestures similar) code is as follows:

// Create a Gesture recognition object    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];     // two consecutive strikes to identify success    tap.numberoftapsrequired  2;     // need a finger to touch    2 ;     // Add a gesture recognizer object to the corresponding UIView     [Self.iconview Addgesturerecognizer:tap];         // Adding a listening method (identifying the corresponding gesture will listen to the event)    [Tap addtarget:self Action: @selector (Btclick:)];

The use of touch events for iOS and the difference from gesture recognizers

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.