Ios ui (Advanced 04 and iosui)

Source: Internet
Author: User

Ios ui (Advanced 04 and iosui)

  • Event Response

    • Recipient

      • In iOS, not all objects can process events. Only objects that inherit UIResponder can receive and process events. We call it the "response object"

      • UIApplication, UIViewController, and UIView are all inherited from UIResponder. Therefore, they are all responder objects that can receive and process events.

    • UITouch

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

      • One finger corresponds to a UITouch object

      • Role of UITouch

        • Stores finger-related information, such as the touch position, time, and 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: iPhone development is in progress. Avoid Double-click events!

      • UITouch attributes

      • + The window where the touch is generated @ property (nonatomic, readonly, retain) UIWindow * window; + the view where the touch is generated @ property (nonatomic, readonly, retain) UIView * view; + The number of screen-based 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, in seconds @ property (nonatomic, readonly) NSTimeInterval timestamp; + the status of the current touch event @ property (nonatomic, readonly) UITouchPhase phase;
      • UITouch Method
      • -(CGPoint) locationInView :( UIView *) view; return value indicates the position of the touch on the view. Here, the returned position is for the coordinate system of the view (starting from the upper left corner of the view (0, 0) if the input view parameter is nil during the call, 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.
      • UIResponder
      • UIResponder provides the following internal methods to handle event touch events-(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;
      • Processing of touch events in UIView
      • UIView is a subclass of UIResponder. It can cover the following four methods to process one or more fingers of a different touch event and start to touch the view. The system will automatically call the following method of view-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) when one or more fingers move an event on the view, the system automatically calls the following method of the view (as the fingers move, will continuously call this method)-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) one or more fingers leave the view, the system automatically calls the following method of view-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) A system event (for example, call in) before the event ends) when the touch process is interrupted, the system automatically calls the following method of view-(void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event prompt: all objects stored in touches are UITouch objects.

         

      • UIEvent
      • Every time an event is generated, A UIEvent object, called an event object, is generated to record the time and type of the event. Common attribute event type @ property (nonatomic, readonly) UIEventType; @ property (nonatomic, readonly) UIEventSubtype subtype; 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: 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 *) Processing Methods of event 4 touch events, both NSSet * touches and UIEvent * event parameters generate only one event object during a complete touch process, 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 one by one, the view calls the touchesBegan: withEvent: method twice, in addition, the touches parameter for each call only contains one UITouch object. Based on the number of UITouch objects in touches, you can determine whether it is single-point touch or multi-point touch.

        Event generation and transmission

        • After a touch event occurs, the system adds the event to an event queue managed by UIApplication.

        • 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) first)

        • 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.

        • After a proper view control is found, the touches method of the View control is called for specific event processing.

          • TouchesBegan...
          • TouchesMoved...
          • TouchedEnded...

      • Three cases where UIView does not receive touch events

        • Do not receive user interaction userInteractionEnabled = NO

        • Hide hidden = YES

        • Transparent alpha = 0.0 ~ 0.01

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

      • 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 at the top level 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 is not processed, it will pass the event or message to the UIApplication object.
          • If UIApplication cannot process this event or message, discard it.
      • How to listen for touch events

        If you want to listen to the touch event on a view, the previous method is

        • Customize a view

          Implement the touches method of view and implement the specific processing code inside the Method

          Using the touches method to listen to view touch events has several obvious disadvantages.

        • You must customize the view.
        • Because the touch event is monitored in the touches method inside the view, by default, it is not easy for other external objects to listen to the touch event of the view to distinguish the specific gesture behavior of the user.
        • After iOS 3.2, Apple launched the Gesture Recognizer feature, which greatly simplifies developer development in terms of touch event processing.

      • UIGestureRecognizer

      • To complete Gesture Recognition, UIGestureRecognizer must use UIGestureRecognizer to easily identify some common gestures UIGestureRecognizer made on a view. UIGestureRecognizer is an abstract class that defines the basic behavior of all gestures, you can use its subclass to process the specific gesture seek (hitting) Seek (kneading, used for scaling) UIPanGestureRecognizer (drag) Seek (Light Scan) UIRotationGestureRecognizer (rotation) UILongPressGestureRecognizer (long press)
      • UITapGestureRecognizer
      • The usage of each gesture identification tool is similar. For example, the procedure for using UITapGestureRecognizer is as follows: Create a gesture identification object UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init]; set the attributes of the gesture reader object. // tap the object twice in a row. numberOfTapsRequired = 2; // you need two fingers to strike the tap together. numberOfTouchesRequired = 2; Add the gesture reader to the corresponding view [self. iconView addGestureRecognizer: tap]; listener [tap addTarget: self action: @ selector (tapIconView :)];
      • Gesture Recognition Status
      • Typedef NS_ENUM (NSInteger, UIGestureRecognizerState) {// no touch event occurs, default status of all Gesture Recognition is unknown, // when a gesture has started but has not been changed or completed, UIGestureRecognizerStateBegan, // The gesture status changes to UIGestureRecognizerStateChanged, // The gesture completes UIGestureRecognizerStateEnded, // The gesture is canceled and restored to the Possible status failed, // The gesture fails and is restored to the Possible status failed, // recognize the gesture recognition UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded };

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.