IOS-event handling for controls responding to user control events

Source: Internet
Author: User

IOS-event handling for controls responding to user control events
Not all objects in iOS can process events. Only objects that inherit UIResponder can receive and process events. We call it "responder object". UIApplication, UIViewController, and UIView all inherit from UIResponder. Therefore, they are all responder objects, the following methods are provided to handle events. These classes can overwrite the following methods to handle different touch events ''' objc // void) vertex :( 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 ;// Void motionBegan :( UIEventSubtype) motion withEvent :( UIEvent *) event; (void) motionEnded :( UIEventSubtype) motion withEvent :( UIEvent *) event; (void) motionCancelled :( UIEventSubtype) motion withEvent :( UIEvent *) event; // void remoteControlReceivedWithEvent :( UIEvent *) event; the parameter UITouchUITouch of the '''event stores finger-related information, for example, when the touch position, time, and phase move the finger, 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 UITouch object UIT Ouch method-(CGPoint) locationInView :( UIView *) view; the returned value indicates the position of the touch on the view. If the returned position is the coordinate system of the view (with the top left corner of the view as the origin (0, 0, the returned result is the position of the touch point in the UIWindow-(CGPoint) previuslocationinview :( UIView *) view. This method records the position of the previous touch point. Every time a UIEvent is generated, A UIEvent object, called an event object, is generated to record the time and type of events. Common properties: event type @ property (nonatomic, readonly) UIEventType; @ property (nonatomic, readonly) UIEventSubtype subtype; event generation time @ property (nonatomic, Readonly) NSTimeInterval timestamp; after a touch event is generated and transmitted, the system adds the event to an event queue managed by UIApplication. 2. UIApplication extracts the first event from the event queue and distributes the event for processing. Generally, it sends the event to the application's main window (keyWindow) 3. the main window will find the most appropriate view in the view hierarchy to process touch events, which is also the first step in the entire event processing process 4. after a proper view control is found, the touches method of the View control is called to handle specific events... TouchesMoved... TouchedEnded... If the parent control cannot receive touch events, the Child control cannot receive the touch events (master). How can I find the most appropriate control to handle events? Can I receive touch events? Are the touch points on yourself? Traverse the child control from the back to the front and repeat the previous two steps. If there is no child control that meets the conditions, the best processing principle is applied.

// Point: the position of the touch point on the method caller's coordinate system-(UIView *) hitTest :( CGPoint) point withEvent :( UIEvent *) event {// 1. determine whether or not to receive the touch event if (self. userInteractionEnabled = NO | self. hidden = YES | self. alpha <= 0.0) return nil; // 2. determine if ([self pointInside: point withEvent: event] = NO) return nil; // 3. traverse the child control int count = (int) self from the back. subviews. count; for (int I = count-1; I> = 0; I --) {// retrieve the child control UIView * childView = self. subviews [I]; // converts it to the coordinate system of the Child control. point CGPoint childP = [self convertPoint: point toView: childView]; UIView * fitView = [childView hitTest: childP withEvent: event]; if (fitView) {return fitView;} // It indicates that there is no more appropriate view return self ;}

 

Complete process of event transfer 1> first pass the event object from top to bottom (the parent control is passed to the Child control) and find the most appropriate control to process the event. 2> call touches .... Method 3> If [super touches…] is called; The event will be passed up along the responder chain, passed to the previous responder 4> then the touches of the previous responder will be called .... Method to Determine the previous responder 1> if the current view is the Controller view, the controller is the previous responder 2> if the current view is not the Controller view, the parent control is the event Transfer Process of the previous responder chain. If the view Controller exists, it is passed to the Controller. If the Controller does not exist, the parent view passed to it is in the top-level view of the view hierarchy. If the received event or message cannot be processed, it will pass the event or message to the window object for processing. If the window object is not processed, it will pass the event or message to the UIApplication object. If the UIApplication cannot process the event or message, then, discard the responder chain. It is a chain that is connected by multiple responder objects. You can clearly see the relationship between each responder, in addition, multiple objects of an event can be processed. Responder object: The method of listening to touch events for objects that can process events. If you want to listen to the touch events in a view, the previous method is to customize a view to implement the touches method of the view, implementing specific processing code inside the method listens to view touch events through the touches method, which has obvious disadvantages 1. the view must be customized. Because the touches method inside the view listens for touch events, by default, it is not easy for other external objects to listen to view touch events to distinguish users' specific gesture behavior UIGestureRecognizer. To complete Gesture Recognition, UIGestureRecognizer uses UIGestureRecognizer, UIGestureRecognizer is an abstract class that defines the basic behavior of all gestures. You can use its subclass to process specific gestures UITapGestureRecognizer (hitting) UIPinchGestureRecognizer (kneading, used for scaling) UIPanGestureRecognizer (drag and drop) rotate (Light Scan) UIRotationGestureRecognizer (rotation) UILongPressGestureRecognizer (long press)

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.