iOS Learning notes-touch and gesture

Source: Internet
Author: User

Touch control

This part of the content has been learned a long, hate remember very late, busy, lazy! This article is simple and remember Yan, afraid of later forget also.

In the touch event of iOS, there are three roles of touch, event, and responder, one touch represents the information contained in the action of one finger and screen touch, and the event contains all the touch information of the entire process of a finger in the entire screen touch, in C # It is easy to think of as a pointer to a method (or function), but it is not, in this case, that both touch and event are used to store information. And the responder is actually able to deal with real touch events, the three roles in OC correspond to the three classes of Uitouch,uievent and Uiresponder respectively. While Uiresponder is actually the base class for all responders, it defines some programming interfaces for common responders. UIApplication and UIView inherit the Uiresponder class, then all inherited UIView classes inherit Uiresponder, in other words, the controls inside the Uikit can respond to finger-to-screen touch events.

The event bubbling in iOS and the principle in Android are roughly the same, but the order is the opposite, whenever the finger touches the screen, the first time to make the corresponding is the topmost view control, that is, the entire visual tree inside the leaf node, and then follow the visual tree has been traced, Along the way to find whether there are objects to deal with this event, the processing has stopped upstream, and finally reached UIWindow and uiapplication, if the uiapplication still can't handle this event, the app will think it does not have the ability to deal with this event, And throw this thing away.

The actual events are described below, and iOS has three events to handle three different touch stages according to the different stages of touch

Touchesbegan:withevent: Triggers when one or more fingers touch the screen

Touchesmoved:withevent: When one or more fingers move on the screen

Touchesended:withevent: When a live multiple finger leaves the screen

There is also a more special touch event that is triggered by a touchescancelled:withevent when the system event is canceled by any stage of the finger touch:

The following example shows the label in the upper-left corner by the position of the finger on the screen

Storyboard controls in Viewcontroller to create associated properties

@interface** *lbup; @end

Then add the following four methods, the first method Touchlog is used for output log information, the content of the log is the current touch is how many touch points, and the current touch stage, Uitouch property phase represents the stage, it is an enumeration uitouchphase, the enumeration value is as follows

    • Uitouchphasebegan, Finger touch screen
    • Uitouchphasemoved, the finger moves on the screen.
    • Uitouchphasestationary, the finger is touching the surface, but because the previous events did not take place to move.
    • uitouchphaseended, finger away from the screen.
    • uitouchphasecancelled, remove the trace touch because when (for example) the user will device to his or her face
-(void) Touchlog: (Uitouch *) Touch{nslog (@ "touch tapcount%d", Touch.tapcount); NSLog (@ "Touch phase%d", Touch.phase);}

The next several methods are described above the touch event method, the content of the three method is roughly the same, is obtained from uievent inside the current event all the touch point Uitouch, through the form of traversal to call the above defined output touch point information method Touchlog, Then, the coordinates of the finger initially touching the screen, the current coordinates of the movement, and the coordinates of lifting the finger are respectively output to the three labels in the View interface.

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{ for(Uitouch *tinch Event. Alltouches) {[Self touchlog:t];}if(touches.count>1)return; Uitouch*touch=[[Event. alltouches Allobjects]objectatindex:0]; Cgpoint locinsself=[Touch Locationinview:self.view];self.lbstart.text=[[nsstring Alloc]initwithformat:@"where to start clicking is: (%2.3f,%2.3f)", locinsself.x, Locinsself.y];} -(void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{ for(Uitouch *tinch Event. Alltouches) {[Self touchlog:t];}if(touches.count>1)return; Cgpoint locinsself=[[[Event. alltouches Allobjects]objectatindex:0] Locationinview:self.view];self.lbmove.text=[[nsstring Alloc]initwithformat:@"The Moving position is: (%2.3f,%2.3f)", locinsself.x, Locinsself.y];} -(void) touchesended: (Nsset *) touches withevent: (Uievent *)Event{ for(Uitouch *tinch Event. Alltouches) {[Self touchlog:t];}if(touches.count>1)return; Cgpoint locinsself=[[[Event. alltouches Allobjects]objectatindex:0] Locationinview:self.view];self.lbup.text=[[nsstring Alloc]initwithformat:@"the position of lifting the finger is: (%2.3f,%2.3f)", locinsself.x, Locinsself.y];}

The output log and interface are shown in the following two images

Gesture

The touch controls above are the most basic for finger touch screen triggering events, and some fixed gestures have been given meaning to touch-screen phones, which can be implemented with touch events, but iOS also provides a simpler way to implement them. In fact, it is a gesture uigesturerecognizer, but it is only an abstract class, to achieve a specific gesture operation, but also to use its sub-class to do, then the following first to explain what gestures for iOS

1. Tap (TAP)

Used to press or select a control or item (similar to a mouse click action),

2. Drag (Drag)

Drag to implement scrolling of some pages, as well as the movement of controls.

3. Slide (Flick)

Swipe for the ability to quickly scroll and page through pages.

4. Sweep (Swipe)

Sweep gesture the shortcut menu for activating list items

5. Double click (double tap)

A mouse-like double-click action, typically used to zoom in and center the picture, or restore the original size if it is currently zoomed in. Also, double-click to activate the Edit menu for text.

6. Zoom in (Pinch Open)

Zooming in gestures enables you to open a feed and open the details of the article. Enlarges the currently viewed picture or text content.

7. Zoom Out (Pinch close)

To zoom out, you can implement features that are reversed and correspond to the magnification gesture: turn off the feed to exit to the first page, and close the article to the index. Reduce the current view of the picture or text content.

8. Long press (Touch &hold)

On my Subscriptions page, long press feeds will automatically go into edit mode, while selecting the feeds that your finger is currently pressing. You can then drag the feed to move the location directly.

With the long press on the text, the Magnifier accessibility feature appears. When released, the Edit menu appears.

Long press on the picture, the Edit menu appears.

9. Shaking (Shake)

Shake gesture, the undo and Redo menu will appear. is primarily for user text input.

For the above 9 gestures, iOS has been integrated through six classes to achieve

    • UITapGestureRecognizer
    • Uipinchgesturerecognizer
    • Uirotationgesturerecognizer
    • Uiswipegesturerecognizer
    • Uipangesturerecognizer
    • Uilongpressgesturerecognizer

They represent taps (click, have a double click), Pinch (pinch, have zoom in and out), Rotation (rotate), Swipe (sliding, fast moving, is used to monitor the direction of the slide), Pan (drag, slow movement, is used to monitor the amount of offsets), and Longpress (Long Press)

When using these gestures, you need to implement their corresponding delegate in the Viewcontroller.

@interface Hgnaviview2controller:uiviewcontroller<uigesturerecognizerdelegate>{}@end

Add the following code in the viewdidload

UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector (Simpletab:)]; tap.numberoftouchesrequired=2; [ Self.view Addgesturerecognizer:tap];   *pin=[[Uipinchgesturerecognizer alloc]initwithtarget:self Action: @selector (simplepinch:)];[ Self.view Addgesturerecognizer:pin];

corresponding to the definition of the corresponding method, respectively, when the gesture triggered by the output log

-(void) Simpletab: (UITapGestureRecognizer *) Recognizer{nslog (@ "numberoftouches%d  ", recognizer.numberoftouches);} -(void) Simplepinch: (uipinchgesturerecognizer*) recognizer{nslog (@ "Pinch%f " , Recognizer.scale); Cgpointlocation =[recognizer LocationInView:self.view];}

Tapping on the screen with two fingers will output the following:

When you zoom out on the screen, the following is output

In reality, often there is a situation, a button at the same time there are double-clicks and click, when clicked will trigger the click, double-click will only trigger the double-click, then this way to add two UITapGestureRecognizer, Click on the Numberoftapsrequired property is set to 1, double-click when the numberoftapsrequired is set to 2, the results of the runtime found that the click can be normal click, double-click, both triggered the double-click, also triggered the clicked. This is not allowed, gesture recognition should be mutually exclusive, to resolve this situation requires calling the [A requiregesturerecognizertofail:b] method. The code is modified as follows, either triggering a click when clicked, or triggering a double-click, not double-clicked or both.

Add the following code inside the Viewdidload

uitapgesturerecognizer*=1; [Self.view Addgesturerecognizer:singlerecognizer]; UITapGestureRecognizer* Doublerecognizer =2; [Self.view Addgesturerecognizer:doublerecognizer]; [Singlerecognizer Requiregesturerecognizertofail:doublerecognizer];

Add the following method to the class

-(void) Singletap: (uipinchgesturerecognizer*) recognizer{nslog (@ "singletap "  -(void) Doubletap: (uipinchgesturerecognizer*) recognizer{nslog (@ "  Doubletap");}

iOS Learning notes-touch and gesture

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.