IOS event processing and iOS event processing

Source: Internet
Author: User

IOS event processing and iOS event processing

Event Processing in iOS is very important and rare. When it comes to the Instant Q & A questions, many old birds who have been working for two or three years may not be very professional. Here we will detail the event handling in iOS and the responder chain.

1. Three major events 2. Target audience
  • In iOS, not all objects can process events, but only inheritUIResponderTo receive and process events. We call itRecipient
  • UIApplication, UIViewController, and UIView are all inherited from UIResponder. Therefore, they are all responder objects that can receive and process events.
2.1 The following methods are provided internally by UIResponder to handle events:
  • Touch event
    - (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;
  • Accelerator event
    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;    - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
  • Remote Control event
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;
2.2 handle touch events of UIView
  • UIView is a subclass of UIResponder. It can overwrite the following four methods to process different touch events.
// Start to touch the view with the root or multiple fingers. The system automatically calls the following method of view-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event // if 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 fingers move)-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event // when one or more fingers exit the view, the system automatically calls the following method of view-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event // before the touch ends, a system event (such as a call-in CALL) will interrupt the touch process, and the system will automatically call the following method of view-(void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event

Tip: all objects in touches are UITouch objects.

2.3 UITouch
  • When you touch the screen with a finger, a UITouch object associated with the finger is created.

  • ** A finger corresponds to a ** UITouch object

Role of UITouch
-Stores finger-related information, such asLocation,Time,Phase

  • When the finger moves, the system updates the same UITouch object so that it can keep the touch position of the finger.

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

    Tip: avoid double-click events during iPhone Development

UITouch attributes

// The window in which the touch is generated @ property (nonatomic, readonly, retain) UIWindow * window; // The view in which the touch is generated @ property (nonatomic, readonly, retain) UIView * view; // The number of screen times in a short period of time. You can determine whether to click, double-click, or click More @ property (nonatomic, readonly) NSUInteger tapCount Based on tapCount; // records the time when a touch event is generated or changed. Unit: seconds. Rarely @ property (nonatomic, readonly) NSTimeInterval timestamp; // The status of the current touch event @ property (nonatomic, readonly) UITouchPhase phase;

UITouch Method

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

  • The returned value indicates the position of the touch on The view.
  • The position returned here is the coordinate system of the view (starting from the upper left corner of the view (0, 0 ))
  • If the view parameter passed in during the call is nil, the returned point is the position of the touch point in the UIWindow.

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

  • This method records the position of the previous touch point.
2.4 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 attributes
1. Event Type
@property(nonatomic,readonly) UIEventType type;
@property(nonatomic,readonly) UIEventSubtype subtype;

2. Event generation time
@property(nonatomic,readonly) NSTimeInterval timestamp;

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

Touches and event parameters

A complete touch process goes through three states:
Start with touch:-(Void) touchesBegan :( NSSet) Touches withEvent :( UIEvent) Event
Touch mobile:-(Void) touchesMoved :( NSSet) Touches withEvent :( UIEvent) Event
Touch ends:-(Void) touchesEnded :( NSSet) Touches withEvent :( UIEvent) Event
Touch cancel:-(Void) touchesCancelled :( NSSet) Touches withEvent :( UIEvent) Event

  • Each of the four touch event processing methods has two parameters: NSSet * touches and 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, and the touches parameter for each call contains only one UITouch object.

  • Based on the number of UITouch in touches, you can determine whether it is single-point touch or multi-point touch.

3. Event generation and transfer 3.1 rule 3.2 event transfer example

The Touch event is transmitted from the parent control to the Child control.

Click the Green view:
UIApplication-> UIWindow-> White-> green
Click the Blue view:
UIApplication-> UIWindow-> White-> orange-> blue
Click the yellow view:
UIApplication-> UIWindow-> White-> orange-> blue-> yellow

If the parent control cannot receive touch events, the Child control cannot receive touch events (master)

  • How to find the most appropriate control to handle events:

Tip: userInteractionEnabled of UIImageView is NO by default. Therefore, UIImageView and its sub-controls cannot receive touch events by default.

4. detailed process of handling 4.1 touch events 4.2 responder chains
  • Responder chain: A chain connected by multiple responder objects
  • Role: You can clearly see the relationship between each responder and process multiple objects of an event.
  • Response object: The object that can process the event
4.3 Complete Event Transfer Process4.4 event Transfer Process of the responder chain 5. Examples

Implementation in the following case:
The yellow View is on top of the button, and the transparency of the View is 0.5. Now, when you click the View and on the button, the response button is required.

# Import "YellowView. h "@ interface YellowView () @ property (nonatomic, weak) IBOutlet UIButton * btn; @ end @ implementation YellowView // used to test whether the UIView has been clicked-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {NSLog (@ "% s" ,__ func _);}-(UIView *) hitTest :( CGPoint) point withEvent :( UIEvent *) event {// coordinates the current View to the Button CGPoint btnP = [self convertPoint: point toView: _ btn]; // determine whether the current point is not on the button. if the current point is on the button, return the button if ([_ btn pointInside: btnP withEvent: event]) {return _ btn ;} else {return [super hitTest: point withEvent: event] ;}}@ end

Test results:
1. Click the area outside the Button within the yellow View to print

2. click the Button. The title Color of the Button changes, indicating that the Button has responded.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.