IOS learning notes 9-ios touch event monitoring and operations

Source: Internet
Author: User

Various operation events will be encountered in iOS development, and these events can be responded through the program.

First, when an event responds, you must know who will respond to the event. In iOS, the responder chain responds to events. All Event Response classes are subclasses of uiresponder. The responder chain is a hierarchical structure composed of different objects, each object obtains the chance to respond to event messages in sequence. When an event occurs, the event is first sent to the first responder. The first responder is the view of the event, that is, the place where the user touches the screen. Events will be passed down along the responder chain until they are accepted and processed.


The road map is like first responser --> the window --> the application --> app delegate


A normal responder chain process is often interrupted by a delegate. An object (usually a view) may delegate the response to another object (usually a View Controller viewcontroller ), this is why the corresponding protocol must be implemented in viewcontroller for event response.


1. Touch and event

-- Uitouch

A uitouch object is created when a finger contacts the screen and moves or leaves the screen. It has several attributes and instance methods:

Phase: attribute. A stage constant is returned, indicating that the touch starts, continues, ends, or is canceled, which corresponds to uitouchphasebegan, uitouchphasemoved, and so on.

Tapcount: attribute, the number of times the screen is pressed.

Timestamp: attribute, time when a touch occurs

View: attribute. The view where the touch starts.

Window: attribute. The window in which the touch starts.

Lacationinview: method, touch the current position in the specified view

Previuslocationview: method. Touch the previous position in the specified view.


-- Uievent

The uievent object contains a set of related uitouch objects, which are composed of uitouch objects. A complete touch operation is a uievent, each of these complete operations is uitouch (press, move, and exit ).

Uievent provides a list of related touch operations. If you want to obtain the touch gesture on the screen, you can use this object. These Column Operations are stored in the nsset object in the foundation framework.


-- Uiresponder

Uiresponser provides four event response methods, which are the core of event capture. These methods receive two parameters: nsset (including the touch of the specified stage) and uievent.

Touchesbegan: withevent: reports the uitouchphasebegan event when you touch your finger on the screen.

Touchesmoved: withevent: reports the uitouchphasemoved event when the finger moves on the screen.

Touchesended: withevent: reports the uitouchphaseended event when the finger leaves the screen.

Touchescancelled: withevent: reports a uitouchphasecancelled event when a call is canceled due to a call or other reason


Define a method to respond to a touch event:

-(void)manageTouches:(NSSet *)touches{    for (UITouch *touch in touches) {        switch (touch.phase) {            caseUITouchPhaseBegan:                NSLog(@"began...");                                break;                            caseUITouchPhaseMoved:                 NSLog(@"move...");                       break;                            caseUITouchPhaseEnded:                 NSLog(@"end...");                       break;            default:                break;        }    }}

Then, in viewcontroller, the method should be as follows:

-(Void) touchesbegan :( nsset *) touches withevent :( uievent *) event;


-(Void) touchesmoved :( nsset *) touches withevent :( uievent *) event;


-(Void) touchesended :( nsset *) touches withevent :( uievent *) event;


For more information about multi-touch, see the apple ios sdk development documentation. multi-touch is usually used in games and some novel applications.


The uicontrol object will be introduced next time.

To join our QQ group or public account, see: Ryan's
Zone public account and QQ Group


Welcome to my Sina Weibo chat: @ Tang Ren _ Ryan

Related Article

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.