-Handling of gesture touch events

Source: Internet
Author: User
Question 1: What events are there in IOS? • Various events are generated when a user uses an app. • Events in IOS can be divided into three major types. Question 2: What is a responder object? • In iOS, not all objects 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 that can receive and process events. Question 3: what internal methods are provided by uiresponder to handle events? • Uiresponder provides the following methods to handle 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 events
- (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;
Question 4: How does uiview handle touch events? • Uiview is a subclass of uiresponder. It can cover the following four methods to handle different touch events. ø one or more fingers start to touch the view. The system automatically calls the following methods of view.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
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
If one or more fingers exit the view, the system automatically calls the following method of the view.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
Ø 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

Tip: all objects in touches are uitouch objects.

Question 5: What is a uitouch object? What is its function? What about the lifecycle? What internal methods are provided? • When you touch the screen with one finger, A uitouch object associated with the finger is created. • A finger corresponds to a uitouch object. • The role of uitouch stores information related to the finger, for example, the position, time, and phase of the touch • lifecycle: ø when the finger moves, the system updates the same uitouch object, to keep the touch position of the finger intact. The system will destroy the method provided in the uitouch object when the finger leaves the screen.
- (CGPoint)locationInView:(UIView *)view;

 

The return value indicates the position of the touch on the view. The returned position is for the coordinate system of the view (the origin is the top left corner of the view (0, 0 )) if the view parameter passed in during the call is nil, the returned value is the position of the touch point in the uiwindow.
- (CGPoint)previousLocationInView:(UIView *)view;
This method records the location of the previous touch point. Note: In iPhone development, avoid double-click events! Question 6: What is uievent? • Each time an event is generated, A uievent object is generated. • uievent is called an event object, which records the time and type of the event. • common attributes ø Event Type
@property(nonatomic,readonly) UIEventType     type;@property(nonatomic,readonly) UIEventSubtype  subtype;
Ø event generation time
@property(nonatomic,readonly) NSTimeInterval  timestamp;
• Uievent also provides corresponding methods to obtain the functions of the touch object (uitouch) on a view. Question 7: touches and event parameters • a complete touch process, there will be three statuses: ø touch start:-(void) touchesbegan :( nsset *) touches withevent :( uievent *) event ø touch movement:-(void) touchesmoved :( nsset *) touches withevent :( uievent *) event ø touch ended:-(void) touchesended :( nsset *) touches withevent :( uievent *) event ø touch canceled (may experience):-(void) touchescancelled :( nsset *) touches withevent :( uievent *) event • each of the four touch event processing methods has an nsset * t Ouches and uievent * event parameters ø only one event object is generated 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, we can determine whether it is single-point touch or multi-point touch. 8: how can we generate and transmit events? • 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 distribute the event for processing. Generally, send the event to the application's main window (keywindow). • The main window will find the most appropriate view in the view hierarchy to process the touch event, this is also the first step in the entire event processing process • after finding the appropriate view control, it will call the touches method of the View Control for specific event processing... Ø touchesmoved... Ø touchedended... • The touch event is transmitted from the parent control to the Child control. ø the green view is clicked:

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). PS: three cases where uiview does not receive touch events. 1. User interaction is not received.

Userinteractionenabled = No

2. Hide

Hidden = Yes

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

• How to find the most appropriate control to handle events? Can I receive touch events? Do the touch points belong to you? Ø 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, it is best for you to handle problem 9: detailed process of processing touch events? • A touch event generated when you click on the screen. After passing through some columns, you will find the most appropriate view control to handle this event. • after finding the most appropriate view control, the touches method of the control is called to handle specific events... Ø touchesmoved... Ø touchedended... • The default method of these touches methods is to transfer the event along the responder chain and send the event to the last responder for handling. Question 10: The Event Transfer Process of the responder chain

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 Question 11: how to listen for touch events uigesturerecognizer listens for touch events • If You Want to listen for touch events on a view, the previous practice was to define a view. The touches method for implementing the view is implemented, and the specific processing code is implemented within the method. • The touches method is used to listen to the view touch event, there are several obvious disadvantages: You must customize the view. Because the touches method inside the view listens to touch events, by default, unable to allow other external objects to listen to the view's touch events ø it is not easy to differentiate users' specific gesture behaviors • after IOS 3.2, Apple launched the gesture recognizer ), in terms of touch event processing, it greatly simplifies developer development difficulty uigesturerecognizer • to complete Gesture Recognition, you must use the gesture identification tool uigesturerecognizer • Use uigesturerecognizer, it can easily recognize some common gestures made by users on a view (PS: the usage of the gesture reader (the usage of each gesture reader is almost the same) PS:
# Pragma mark-proxy method of the gesture Reader/*** whether multiple gesture identifiers are allowed to work simultaneously * simultaneously: simultaneously */-(bool) gesturerecognizer :( uigesturerecognizer *) gesturerecognizer shouldrecognizesimultaneouslywithgesturerecognizer :( uigesturerecognizer *) othergesturerecognizer {return yes ;}

 

• Uigesturerecognizer is an abstract class that defines the basic behavior of all gestures. You can use its subclass to process specific gestures. 1> uitapgesturerecognizer (hitting)
Ø create a gesture identification object uitapgesturerecognizer * tap = [[uitapgesturerecognizer alloc] init]; ø set the specific attributes of the gesture identification object // Tap 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 gesture trigger [tap addtarget: Self action: @ selector (tapiconview :)];
2> uipinchgesturerecognizer (Kneading for scaling)
# Pragma mark-zoom gesture (pinch gesture)-(void) testpinch {uipinchgesturerecognizer * pinch = [[mongoalloc] initwithtarget: Self action: @ selector (pinchview :)]; pinch. delegate = self; [self. iconview addgesturerecognizer: pinch];}-(void) pinchview :( uipinchgesturerecognizer *) pinch {pinch. view. transform = cgaffinetransformscale (pinch. view. transform, pinch. scale, pinch. scale); pinch. scale = 1; // This is true Is very important !!!!!}
3> uipangesturerecognizer (drag and drop)
-(Void) viewdidload {[Super viewdidload]; uipangesturerecognizer * Pan = [[uipangesturerecognizer alloc] initwithtarget: Self action: @ selector (panview :)]; [self. purpleview addgesturerecognizer: Pan];}-(void) panview :( uipangesturerecognizer *) Pan {Switch (Pan. state) {Case uigesturerecognizerstatebegan: // start to trigger the gesture break; Case uigesturerecognizerstateended: // stop the gesture break; default: break;} // 1. the distance to cgpoint translation = [Pan translationinview: Pan. view]; cgpoint center = pan. view. center; center. X + = translation. x; center. Y + = translation. y; Pan. view. center = center; nslog (@ "% @", nsstringfromcgpoint (translation); // 2. clear the distance of the Movement [Pan settranslation: cgpointzero inview: Pan. view];}

4> uiswipegesturerecognizer (Light Scan)

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView)];    swipe.direction = UISwipeGestureRecognizerDirectionUp;    [self.redView addGestureRecognizer:swipe];
5> uirotationgesturerecognizer (rotate)
# Pragma mark-rotation gesture-(void) testrotate {uirotationgesturerecognizer * recognizer = [[externalloc] initwithtarget: Self action: @ selector (rotateview :)]; recognizer. delegate = self; [self. iconview addgesturerecognizer: recognizer];}-(void) rotateview :( uirotationgesturerecognizer *) recognizer {recognizer. view. transform = cgaffinetransformrotate (recognizer. view. transform, recognizer. ro Tation); recognizer. Rotation = 0; // This is very important !!!!!}
6> uilongpressgesturerecognizer (long press)
Uilongpressgesturerecognizer * longpress = [[uilongpressgesturerecognizer alloc] init]; [longpress addtarget: Self action: @ selector (longpressview)]; // press longpress for at least 2 seconds. minimumpressduration = 2; // before the gesture is triggered, press the valid longpress within the 50px range. allowablemovement = 50; [self. redview addgesturerecognizer: longpress];

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 };

 

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.