IOS (25) UI touch events, iosui
CAT/CAT sharing, must be excellent
For Original Articles, please reprint them. Reprinted Please note: Yan Nai-yu's blog
Http://blog.csdn.net/u013357243? Viewmode = contents
Events in iOS
Various events are generated when users use the app. Events in iOS can be divided into three types:
Response object-UIResponder
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.
UIResponder provides the following methods 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;
This is mainly a touch event. Acceleration and remote control events can be skipped first.
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. You can implement the following four methods to handle different touch events:
1: one or more fingers start to touch the view. The system automatically calls the following method of view.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
2: 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
3: If one or more fingers exit the view, the system automatically calls the following method of view.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
4: Before the touch ends, a system event (such as incoming call) will interrupt the touch process, and the system will automatically call the following method of view
-(Void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event prompt: all objects stored in touches are UITouch objects.
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.
The role of UITouch.
Stores finger-related information, such as the position, time, and phase of the touch.
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.
Note: In iPhone development, avoid double-click events!
UITouch attributes
The window in which the touch occurs
@property(nonatomic,readonly,retain) UIWindow *window;
View where the touch is generated
@property(nonatomic,readonly,retain) UIView *view;
In a short period of time, you can click, double-click, or more clicks Based on tapCount.
@property(nonatomic,readonly) NSUInteger tapCount;
The time when a touch event is generated or changed, measured in seconds.
@property(nonatomic,readonly) NSTimeInterval timestamp;
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.
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 UIEvent attributes
Event Type
@property(nonatomic,readonly) UIEventType type;@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 Mobile:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
Touch ends:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
Touch cancel (may experience ):
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
1: Four touch event processing methods have two parameters: NSSet * touches and UIEvent * event.
2: Only one event object is generated during a complete touch process. The four touch methods are the same event parameter.
3: 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.
4: If the two fingers touch the same view one by one, the view will call the touchesBegan: withEvent: method twice, the touches parameter for each call only contains one UITouch object.
5: based on the number of UITouch in touches, you can determine whether it is single-point touch or multi-point touch.
Event generation and transmission
1: After a touch event occurs, the system adds the event to an event managed by UIApplication.QueueMedium
2: UIApplication extracts the first event from the event queue and distributes the event for processing. Generally, the event is first sent to the main window (keyWindow) of the application)
3: The main window will find the most appropriate view in the view hierarchy to handle touch events, but this is only the first step in the entire event processing process.
4: After finding an appropriate view control, the touches method of the View control will be called for specific event processing:
TouchesBegan...
TouchesMoved...
TouchedEnded...
Event transfer example
The Touch event is transmitted from the parent control to the Child control.
If you click a Green view (->), it indicates passing:
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?
1> can I receive touch events? No, the event is passed to this end
2> are touch points on yourself? No, the event is passed to this end
3> traverse the child control from the back to the front and repeat the previous two steps.
4> if there is no child control that meets the conditions, it is the most suitable for processing.
Three cases where UIView does not receive touch events
I. do not receive user interaction
UserInteractionEnabled = NO
Ii. Hide
Hidden = YES
3. Transparency
Alpha = 0.0 ~ 0.01
In these three cases, UIView is unacceptable.
Note: userInteractionEnabled of UIImageView is NO by default. Therefore, UIImageView and its sub-controls cannot receive touch events by default.
Detailed process of processing touch events
A touch event generated when a user clicks the screen. After a series of transfer processes, the most appropriate view control is found to process the event. After finding the most appropriate view control, the touches method of the control will be called for specific event processing.
TouchesBegan... TouchesMoved... TouchedEnded... The default method of these touches methods is to pass events up the responder chain and send the events to the last responder for processing.
Complete process:
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 the responder chain and passed to the last responder.
4> then, the touches of the last responder will be called .... Method
Responder chain
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
Event Transfer Process of the responder chain
1: If the view Controller exists, it is passed to the Controller. If the Controller does not exist, it is passed to its parent view.
2: In the top-level view 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.
3: If the window object is not processed, it will pass the event or message to the UIApplication object.
4: If the UIApplication cannot process the event or message, discard it.
How to determine the last Responder
1> if the current view is the Controller view, the controller is the last responder.
2> if the current view is not the Controller view, the parent control is the last responder.
How to listen for touch events
If you want to listen to the touch event on a view, the previous method is
1: Customize a view.
2: implement the touches method of view and implement the specific processing code within the method.
3: using the touches method to listen to view touch events has several obvious disadvantages.
A: You must customize the view.
B: Because the touch event is monitored in the touches method inside the view, other external objects cannot be monitored by default.
C: It is not easy to differentiate users' specific gesture behaviors.
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, you must use the UIGestureRecognizer.
Using UIGestureRecognizer, you can easily identify common gestures you make on a view.
UIGestureRecognizer is an abstract class that defines the basic behavior of all gestures. You can use its subclass to process specific gestures.
UITapGestureRecognizer compaction (Kneading for scaling) UIPanGestureRecognizer UIRotationGestureRecognizer UILongPressGestureRecognizer (long press)
UITapGestureRecognizer
The usage of each gesture reader is similar. For example, the steps for using UITapGestureRecognizer are as follows:
Create a gesture identification object
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
Set the attributes of the gesture reader object
// Strike twice in a row
tap.numberOfTapsRequired = 2;
// Two fingers need to be tapped together
tap.numberOfTouchesRequired = 2;
Add the gesture reader to the corresponding view.
[self.iconView addGestureRecognizer:tap];
Trigger of listening gesture
[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 };
Gesture Recognition status changes:
Ps: new iOS communication learning group: 304570962 can be added to cat qq: 1764541256 or znycat. Let's study hard together.
Yan Nai-yu's blog
Http://blog.csdn.net/u013357243? Viewmode = contents