iOS Learning Note (2)-uiview User Incident Response
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; Remove a particular view or touch object on the 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.
-(
void
) touchesEnded:(
NSSet
*)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if
(touch.tapCount == 1)
{
[
self performSelector:
@selector
(setBackground:) withObject:[UIColor blueColor] afterDelay:2];
}
else
if
(touch.tapCount == 2)
{
[
self
cancelPreviousPerformRequestsWIthTarget:
self
selector:
@selector
(setBackground:) object:[UIColor blueColor]];
self
.view.backgroundColor = [UIColor redColor];
}
}
|
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 return No
-(BOOL) Becomefirstresponder
-(BOOL) Canresignfirstresponder default return Yes
-(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
Property:
Nsarray *gesturerecognizers
This property allows you to get all the gesture objects for the current UIView. Gestures are in the role of the observer in the Touch event processing process, which is not part of the view hierarchy and therefore does not participate in the responder chain. Touch events are sent to the gesture recognizer of the view bindings before the touch event is delivered to Hit-test view.
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. The base class for gesture recognition is Uigesturerecognizer, an abstract class that defines the programming interface for implementing the underlying gesture recognition behavior. 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: initialization 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. When the gesture state is set to this it is worth the time to cancel the gesture.
When Cancelstouchesinview is yes, it means that when gesture recognizers recognizes the gesture, touchescancelled is sent to Hit-test view: Message to cancel Hit-test View handles the touch sequence so that only gesture recognizer responds to this touch, and the view of the responder chain no longer responds. If no, the touchescancelled: message is not sent so that gesture recognizer and view respond to the touch event at the same time. The default value is yes.
A touch event is sent to Hit-test view at the same time when the Delaystouchesbegan is no, indicating that the touch sequence has begun and gesture recognition has not yet been recognized for this gesture. If yes, no touch events are sent to Hit-test view during the recognition process, and if the gesture recognizer eventually recognizes the gesture, no message is sent to Hit-test view, and if the gesture recognizer does not eventually recognize the gesture, All touch events are sent to view processing. The default value is No.
When Delaystouchesbegan is yes, delay sending touchesended: message, gesture fails to send. The default value is yes.
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
Method:
-(Cgpoint) Velocityinview: (UIView *) View returns the speed of the drag gesture, the value is the point value moved per second, divided into horizontal and vertical two components.
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, and the unit is pixel pixels.
Multi-gesture Compatible
You can add multiple gestures to a view, and by default, the order of gestures is not ordered, and each call order may be different. You can control the order in which gestures are responded by using the following methods.
-(void) Requiregesturerecognizertofail:(Uigesturerecognizer *) Othergesturerecognizer
The gesture does not occur after a gesturerecognizer as a parameter, otherwise the gesture never occurs.
[Self.panrecognizer RequireGestureRecognizerToFail:self.swipeRecognizer]; A pinch gesture fails before a drag gesture is triggered. If the pinch gesture succeeds, the drag gesture will never be triggered.
-(BOOL) Canbepreventedbygesturerecognizer: (Uigesturerecognizer *) Preventinggesturerecognizer
This method can be overloaded, such as the subclass of Uigesturerecognizer overloads this method to return no, that is, in any case the class gesture can not be blocked, is a very strong gesture.
If yes is returned, the Preventinggesturerecognizer incoming gesture organizes the sub-class gestures. Like what:
[Rotationgesturerecognizer Canbepreventedbygesturerecognizer:pinchgesturerecognizer]; If the rotation gesture overloads the Canbepreventedbygesturerecognizer method and returns Yes. The rotation gesture is blocked by a pinch gesture, but the rotation gesture does not prevent the pinch gesture.
Logical judgments can also be added to the method body.
-(BOOL) Canpreventgesturerecognizer: (Uigesturerecognizer *) Preventedgesturerecognizer
This method can also be overloaded, and if no is returned, this gesture cannot prevent any other gestures.
If you return yes, you can block preventedgesturerecognizer gestures. Like what:
[Rotationgesturerecognizer Canpreventgesturerecognizer:pinchgesturerecognizer]; If the rotation gesture overloads the Canbepreventedbygesturerecognizer method and returns Yes. The rotation gesture prevents the pinch gesture.
Uigesturerecognizerdelegate
-(BOOL) Gesturerecognizershouldbegin: (Uigesturerecognizer *) Gesturerecognizer
This method is called when the gesture Recognizer view is out of uigesturerecognizerstatepossible state, and if no is returned, it is converted to uigesturerecognizerstatefailed If yes is returned, the recognition continues. Return Yes by default
-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) touch
This method is called before the Touchesbegan:withevent: method occurs when a Window object has a touch event. If no is returned, Gesturerecognizer ignores this touch event. Returns Yes by default. A gesture that can be used to suppress an area.
-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer;If more than one gesture receives the same message, the callback method determines whether the current gesture responds to the event, if yes is returned, and if no, the event is ignored
iOS Learning Note (2)-uiview User Incident Response