UIView User Event Response

Source: Internet
Author: User
Tags vars

UIView is responsible for responding to user events in addition to displaying content to users. This chapter focuses on the properties and methods associated with UIView user interaction.

1. Interaction-related properties

userinteractionenabled Default is yes, and if set to no it does not respond to user events and removes the current control from the event queue. That is, the view that sets the Userinterfaceenabled property interrupts the responder chain, causing the view's subview to fail to respond to the event.

multipletouchenabled Default is no, and multi-touch is supported if set to Yes.

Exclusivetouch Default is no, and if set to yes the current UIView will monopolize the entire touch event. Specifically, if UIView set the Exclusivetouch property to Yes, when the UIView becomes the first responder, the other view will not respond to any touch events before the finger leaves the screen.

Example: Each cell in UITableView needs to use exclusive, otherwise clicking multiple cells simultaneously triggers an event response for each view. Gesture recognition ignores this property.

2. Touch response

Before you understand UIView's touch response, first understand what touch events are in iOS, how events are passed in the view model, and how the view responds when an event is received. The following describes the touch event class Uitouch and the responder chain to explain how the event works.

In iOS, the Uitouch class represents a touch event. When the user touches the screen, a corresponding event is generated, and all related Uitouch objects are wrapped in the event and are processed by the program to a particular object. The Uitouch object includes details about the touch.

Uitouch contains 5 properties:

windows: The window in which the touch is generated is not necessarily the first window because the window may change.

View: The point at which the touch occurs. Because the view can change, the current view is not necessarily the original view.

Tapcount: The number of taps (taps) in a short period of time can be judged by Tapcount click, double tap or more.

timestamp: Time Stamp records the time when a touch event has occurred or changed. Unit is seconds.

phase: Touch events have a cycle on the screen, i.e. touch start, touch point movement, end of touch, cancel halfway. The phase lets you see the status of the current touch event in a cycle. Uitouchphase enumeration:

Uitouchphasebegan

Uitouchphasemoved

Uitouchphasestationary

uitouchphaseended

Uitouchphasecancelled

When the finger touches the screen, whether it's a single point or a multi-touch, the event starts until all the user's fingers leave the screen. All Uitouch objects are encapsulated in the Uievent event object and distributed to the processor by the program. Events record the changes in the state of all touches in this cycle.

As long as the screen is touched, the system wraps the touch information into a Uievent object and sends the event to the program, which is distributed by the Hypervisor uiapplication object.

A Responder object is an object that can respond to an event and handle the event. In iOS, the Uiresponder class defines all the methods of the responder object. Controls inherited from Uikit in UIApplication, UIWindow, Uiviewcontroller, UIView, and UIView are indirectly or directly inherited from the Uiresponder class, which can be used as responders.

The responder chain represents a chain of event passes consisting of a series of responder objects. When the first responder is identified, the event is referred to the first responder, and if the first responder does not process the event along the responder chain, it is handed over to the next responder. In general, the first responder is a subclass object of the UIView object, or UIView, when it is handled by the touch event, and if it is not processed, the event is handed to its uiviewcontroller processing (if present) and then its Superview parent view object. And so on until the top-level view. If the top-level view is not processed, it is handed to the UIWindow object processing, and then to the UIApplication object (if UIApplication inherits from Uiresponder). If the entire responder chain does not respond to this event, the event is discarded.

The UIView class inherits the Uiresponder class, and the event handlers defined in the Uiresponder class need to be overridden to handle the event. Depending on the touch state, the program invokes the corresponding handler function, which includes:

-(void) Touchesbegan: (Nsset *) touches withEvents: (Uievent *) event;

-(void) touchesmoved: (Nsset *) touches withEvents: (Uievent *) event;

-(void) touchesended: (Nsset *) touches withEvents: (Uievent *) event;

-(void) touchescancelled: (Nsset *) touches withEvents: (Uievent *) event;

When these methods are called, they correspond to the 4 enumeration values of the phase property in the Uitouch class. The Touchescancelled:touches: method is called when the touch is canceled, such as when a call is interrupted during a touch.

These methods do not need to be fully implemented in development, and you can override specific methods as needed. All 4 methods have two identical parameters: Nsset type touches and uievent type of event. The touches represents all the Uitouch objects that are generated by the touch, and the event represents events. Because the uievent contains all the touch objects throughout the touch, you can call the Alltouches method to get all the touch objects in the event, or you can call Touchesforview, or touchesforwindows; Touch objects on a particular view or window. In these events, you can get a touch object and then do the logical processing based on its position, state, and time attributes.

Tapping an operation can easily cause ambiguity, such as when a user clicks it once and does not know whether the user wants to click or just part of the double-tap, or two times before the user wants to double-click or continue. You can use the deferred call function to solve this problem.

[OBJC]View Plaincopyprint?
  1. -(void) touchesended: (nsset *) touches withevent: (uievent *) event
  2. {
  3.   Uitouch *touch = [touches anyobject];
  4. if (touch. Tapcount = = 1)
  5. {
  6. [self performselector:@selector (setbackground:) withobject:[uicolor Bluecolor] afterdelay:  2];
  7. }
  8. Else if (touch. Tapcount = = 2)
  9. {
  10. [self cancelpreviousperformrequestswithtarget:self selector:@selector (setbackground:)   Object:[uicolor Bluecolor]];
  11. self. View. backgroundcolor = [Uicolor redcolor];
  12. }
  13. }

In addition to touch events, Uiresponder also provides support for motion events.

Methods of motion events:

-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event shakes start

-(void) motionended: (uieventsubtype) Motion withevent: (uievent *) event shakes end

-(void) motioncancelled: (uieventsubtype) Motion withevent: (uievent *) event shaking events interrupted

Remote events:

-(void) Remotecontrolreceivedwithevent: Music background playback control will be used

The correlation function of the first responder:

-(BOOL) Canbecomefirstresponder Default returns no

-(BOOL) Becomefirstresponder

-(BOOL) Canresignfirstresponder return Yes by default

-(BOOL) Resignfirstresponder;

-(BOOL) Isfirstresponder

The Becomefirstresponder method can be registered as the first responder, and the Resignfirstresponder method does not become the first responder. For example, using these two methods to manipulate Uitextfield to control the keyboard is now hidden.

3. Gestures

UIView Methods for gestures:

-(void) Addgesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Add a gesture.

-(void) Removegesturerecognizer: (Uigesturerecognizer *) Geturerecognizer Delete a gesture.

-(BOOL) Gesturerecognizershouldbegan: (Uigesturerecognizer *) Gesturerecognizer asks whether to start the gesture and returns Yes by default.

The advantage of gestures compared to touch events is that you can use the already defined gestures directly, and developers don't have to calculate their own finger movement trajectory.

Uigesturerecognizer is a gesture base class that provides a simple way to implement gestures. The derived classes are as follows:

Uitabgesturerecognizer Tap gestures

Uipinchgesturerecognizer Pinch gesture

Uirotationgesturerecognizer Rotation gesture

Uiswipegesturerecognizer Swipe gesture

Uipangesturerecognizer Pull gesture

Uilongpressgestruerecognizer Long Press gesture

Uigesturerecognizer Main methods:

-(ID) initwithtarget:action: Initialize Method

-(void) Addtarget:action:  

-(void) Removetarget:action:

Main properties:

uigesturerecognizerstate State gesture identifies the current status

There are several situations:

Uigesturerecognizerstatepossibel, unrecognized status

Uigesturerecognizerstatebegan, gesture started.

uigesturerecognizerstatechanged, gesture change

uigesturerecognizerstateended, gesture over.

The uigesturerecognizerstatefailured gesture failed and was interrupted by other events.

Uitabgesturerecognizer Tap gestures any number of clicks of any finger

Property:

numberoftapsrequired Click Count

numberoftouchesrequired Number of fingers

uipinchgesturerecognizer pinch or dilate gesture

Property:

Scale: The initial value is 1, the two finger distance is reduced, and it becomes smaller, and two fingers coincide to 0;

Velocity: The initial value is 0, the relative speed of the finger movement, two finger distance is reduced to a negative number, the faster the value of the less; two finger distance is larger as an integer, the faster the value is greater.

Uirotationgesturerecognizer Rotation gesture

Property:

Rotation: The initial value is 0, the rotational radian of two fingers, clockwise rotation is positive, counterclockwise rotation is negative.

Velocity: The initial value is 0 the relative speed of the finger move, the faster the clockwise is the higher the value, the faster the negative counter-clockwise the smaller.

Uiswipgesturerecognizer Swipe gesture, a gesture can only specify One direction, if multiple gestures are required to specify multiple directions

Property:

numberoftouchesrequired: Number of fingers

Direction: Gesture direction, such as Uiswipegesturerecognizerdirectionright right

Uipangesturerecognizer: Drag gestures that interact with your screen longer than the swipe gesture.

Property:

mininumnumberoftouches: Default value is 1, minimum number of fingers

maxnumnumberoftouches: Maximum number of fingers

Uilongpressgestruerecognizer: Long press gesture.

Property:

numberoftapsrequired: The default value is 0 and the number of taps is clicked.

numberoftouchesrequired: The default value is 1, number of fingers.

mininumpressduration: The default value is 0.5, in seconds.

allowablemovement: The default value is 10, in pixels.

UIView User Event Response

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.