IOS-touch event-event transfer process and response chain, ios-chain

Source: Internet
Author: User

IOS-touch event-event transfer process and response chain, ios-chain

1. There are three common events in ios:

Touch event

Accelerator event

Remote Control event

2. Why does it mean that events can be handled with the inheritance of UIResponder?

This is because the following methods are provided internally by UIResponder to handle events:

For example, a touch event calls the following method:

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event;

-(Void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event;

-(Void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event;

-(Void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event;

The accelerator event is called:

-(Void) motionBegan :( UIEventSubtype) motion withEvent :( UIEvent *) event;

-(Void) motionEnded :( UIEventSubtype) motion withEvent :( UIEvent *) event;

-(Void) motionCancelled :( UIEventSubtype) motion withEvent :( UIEvent *) event;

Remote control events call:

-(Void) remoteControlReceivedWithEvent :( UIEvent *) event;

3. How to monitor the touch events of UIView

The Touch events of UIView mainly include:

One or more fingers start to touch the view. The system automatically calls the following method of view.

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event

When one or more fingers move on the view, the system automatically calls the following method of the view.

(This method will be continuously called as the finger moves, that is, this method will be called many times)

-(Void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event

If one or more fingers exit the view, the system automatically calls the following method of the view.

-(Void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event

Before the touch ends, a system event (such as a call) interrupts the touch process and the system calls the view method.

-(Void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event

Parameter description:

Touches:

Touches stores all UITouch objects, which are a collection of nssets.

The UITouch object is used to save information associated with the finger, including location, time, and phase information.

Each finger corresponds to a UITouch object.

The UITouch is automatically created by the system. When the finger moves, the system updates the same UITouch object,

So that it can keep saving the touch position of the finger

When the finger leaves the screen, the system will destroy the corresponding UITouch object.

UITouch attributes

● The window in which the touch occurs

@ Property (nonatomic, readonly, retain) UIWindow * window

● View where the touch is generated

@ Property (nonatomic, readonly, retain) UIView * view
● In a short period of time, you can click, double-click, or click more based on tapCount.

@ Property (nonatomic, readonly) NSUIntegertapCount;

● Record the time when a touch event is generated or changed, in seconds

@ Property (nonatomic, readonly) NSTimeIntervaltimestamp;

● The status of the current touch event

@ Property (nonatomic, readonly) UITouchPhasephase;

By obtaining the UITouch attribute, we can obtain the window, View, time, and number of clicks when the touch is generated,

These can be obtained through UITouch.

UITouch Method

●-(CGPoint) locationInView :( UIView *) view;

The returned value indicates the position of the touch on The view.

The position returned by Li is the coordinate system of the view (starting from the top left of the view (0, 0 ))

If the value of the "view" parameter passed during the permanent period is nil, the returned value is the position of the touch point in the UIWindow.

-(CGPoint) previuslocationinview :( UIView *) view;

This method records the position of the previous touch point.

UIEvent

● Each time an event is generated, A UIEvent object is generated.
● UIEvent: An event object that records the time and type of an event.

● Common attribute event types

@property(nonatomic,readonly) UIEventType     type;@property(nonatomic,readonly) UIEventSubtype  subtype;

Event generation time
@ Property (nonatomic, readonly) NSTimeIntervaltimestamp;

● UIEvent also provides corresponding methods to obtain the touch object (UITouch) on a view)

Touches and event parameters

A complete touch process goes through three states:

Touch start:-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event

Touch movement:-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event

Touch end:-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event

Touch cancel (may experience):-(void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event

In a complete touch process, only one event object is generated. The four touch methods are the same event parameter.

If two fingers touch a view at the same time, the view will only call the touchesBegan: withEvent: Method once. The touches parameter contains two UITouch objects.

If the two fingers touch the same view separately, the view calls the touchesBegan: withEvent: method twice,

The touches parameter for each call only contains one UITouch object.

Event transfer:

Note:

UIView does not receive touch events.

Do not receive user interaction

UserInteractionEnabled = NO.

Hide

Hidden = YES.

Transparent

Alpha = 0.0 ~ 0.01

Tip: the default value of userInteractionEnabled in UIImageView is NO. Therefore

And its child controls cannot receive touch events by default.

Summary: detailed process of processing touch events

① Find the most appropriate view

After a touch event occurs, the system adds the event to an event queue managed by the UIApplication.
①. UIApplication extracts the first event from the event queue and distributes the event for processing.

Normally, send the event to the main window (keyWindow) of the application first)
② The main window will find the most appropriate view in the view hierarchy to handle touch events, which is also the processing of the entire event

Step 1 of the process

How can we find the most appropriate view?

1. Can I receive touch events?
2. Whether the touch points are on your own
3. traverse the child control from the back and repeat the previous two steps.
4. If there is no child control that meets the conditions, it is the most suitable for processing.

② After finding the most appropriate view control, the touches method of the control will be called for specific events.

TouchesBegan...

TouchesMoved...
TouchedEnded...

③ The default method of these touches methods is to pass the event up the responder chain and send the event to the last responder for processing.

Responder chain

Event Transfer Process of the responder chain

If the view Controller exists, it is passed to the Controller. If the Controller does not exist, it is passed to its parent view.

In the top-level view of the view hierarchy, if the received event or message cannot be processed, the event or message is transmitted to the window object for processing.

If the window object cannot be processed, it will pass the event or message to the UIApplication object. If the UIApplication cannot process the event or message, it will be discarded.

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.