iOS Development Learning touch events and gesture recognition (go

Source: Internet
Author: User

input events for iOSTouch event gesture recognition phone shake one, iOS input eventsTouch Events (swipe, click) motion events (shake, tilt, walk), no man-made remote control events (headphone control phone sound) 1?? iOS event objects are instances of the Uievent class The Uievent class defines an ENUM constant for the event type: TypeDef ns_enum (Nsinteger, Uieventtype) {Uieventtypetouches,Uieventtypemotion, Uieventremotecontrol,}; Touch events must be inherited uiresponser two, touch event 1?? UIView, there are 4 ways to handle different touch events UIView is a subclass of Uiresponder, and you can override the following 4 methods to handle different touch events. 1. One or more fingers begin to touch the screen-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event2. One or more fingers moving on the screen (as the finger moves, The method will be called continuously)-(void) touchesmoved: (Nsset *) touches withevent: (uievent *) event3. One or more fingers left the screen-(void) touchesended: (Nsset * ) Touches withevent: (uievent *) event4. A system event, such as a phone call, interrupts the touch process before the touch is finished-(void) touchescancelled: (Nsset *) touches withevent :(Uievent *) event copy code #pragma mark-uitouch event #pragma mark touch Start-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent * ) event{NSLog (@ "touch start");For (Uitouch *touch in touches) { NSLog (@ "%@", touch);}}#pragma mark touch Move-(void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{NSLog (@ "Touch the mobile Touch object number:%d", [touches count]);To move the yellow color view on the interface1. Get the position of the current fingerUitouch *touch = [touches anyobject];Cgpoint location = [Touch LocationInView:self.view];2. Get the position of the last fingerCgpoint prelocation = [Touch PreviousLocationInView:self.view];3. Calculates the offset between two positionsCgpoint offset = Cgpointmake (location.x-prelocation.x, LOCATION.Y-PRELOCATION.Y);4. Adjust the position of the view using the calculated offset[_demoview setcenter:cgpointmake (_demoview.center.x + offset.x, _demoview.center.y + offset.y)]; A complete Uitouch event debugging methodNSLog (@ "touch move");For (Uitouch *touch in touches) {NSLog (@ "%@", touch);}}#pragma mark touch End-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{A complete Uitouch event debugging methodNSLog (@ "Touch done");For (Uitouch *touch in touches) {NSLog (@ "%@", touch);}}#pragma mark Touch Interrupt-(void) touchescancelled: (Nsset *) touches withevent: (uievent *) event{A complete Uitouch event debugging methodNSLog (@ "Touch interrupt");For (Uitouch *touch in touches) { NSLog (@ "%@", touch);}} Copy code 2?? Touch event Handling If the Hit-test view cannot handle the event, is passed up through the responder chain 1. If the controller of the Hit-test view is present, it is passed to the controller, and if the controller does not exist, it is passed to its parent view 2. If the view or its controller cannot process the received event or message, it is passed to the view's parent view 3. Each upper view in the View inheritance tree cannot be processed If you receive an event or message, repeat the above step 1,24. In the topmost view of the view inheritance tree, if you cannot process the received event or message, it passes the event or message to the Window object for processing 5. If the window object cannot be processed, It passes an event or message to the UIApplication object 6. If UIApplication cannot process the event or message, it is discarded when the user taps the screen, resulting in a Uitouch object passed to UIApplication, which is then the window responsible for finding the most appropriate touch View Object (Hittest,pointinside) to find the appropriate view, the touch method is completed by the corresponding view, the superior view no longer take over 3?? Three methods that do not accept handling events do not receive user interaction: userinteractionenabled = no, hidden: hidden = YES, transparent: Alpha = 0~0.01, gesture recognition 1?? iOS currently supports gesture recognition (6 kinds) UITapGestureRecognizer (tap) UipinchgesturerecognizerKneading Uipangesturerecognizer (drag) Uiswipegesturerecognizer(swipe) UirotationgesturerecogniZer (swivel) UilongpressgesturerecognIzer (Long Press) 2?? The use of gesture recognition (4 steps) is typically defined when the view is loaded (Uigesturerecognizer is an abstract class that needs to be instantiated) to create gesture recognition instances to set gesture recognition properties, such as the number of fingers, direction, etc. attach gesture recognition to a specified view write gesture trigger response Method 3?? Status of gesture recognition (7)1.No touch event occurs, default state of all gesture recognitionUigesturerecognizerstatePossible,When a gesture has started but has not changed or completedUigesturerecognizerstateBegan,Gesture State ChangeUigesturerecognizerstateChanged, Gesture CompletionUigesturerecognizerstateEnded,Gesture cancellation, revert to possible stateUigesturerecognizerstateCancelled,Gesture failed to revert to possible stateUigesturerecognizerstateFailed,Recognition to gesture recognitionUigesturerecognizerstateRecognized =uigesturerecognizerstateEnded2. Gesture recognition Properties state--gesture Status view--gesture occurrence View common method Locationinview get gesture occurrence corresponding view location copy code-(void) viewdidload{[Super Viewdidload];Based on the instantiation method, we know:1. There is an object that handles the message, which should be self2. We need to define a method that, when gesture recognition is detected, runsUITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector (tapaction:)];Setnumberoftapsrequired Click Times[Tap setnumberoftapsrequired:1];SetnumberoftouchesrequirThe number of fingers that Ed has pressed.[Tap SetnumberoftouchesrequirED:1];Add gesture recognition to the view[Self.demoview Addgesturerecognizer:tap]; Uipinchgesturerecognizer*pinch = [[UipinchgesturerecognizerAlloc]initwithtarget:self Action: @selector (pinchaction:)];[Self.demoview Addgesturerecognizer:pinch];UirotationgesturerecogniZer *rotation = [[UirotationgesturerecogniZer alloc]initwithtarget:self Action: @selector (rotationaction:)];[Self.demoview addgesturerecognizer:rotation];Uipangesturerecognizer *pan = [[Uipangesturerecognizer alloc]initwithtarget:self Action: @selector (panaction:)];[Self.demoview Addgesturerecognizer:pan]; UilongpressgesturerecognIzer *longpress = [[UilongpressgesturerecognIzer alloc]initwithtarget:self Action: @selector (longpressaction:)];[Self.demoview addgesturerecognizer:longpress];Swipe leftUiswipegesturerecognizer*swipeleft = [[UiswipegesturerecognizerAlloc]initwithtarget:self Action: @selector (swipeaction:)]; [Swipeleft Setdirection:uiswipegesturerecognizerDirectionleft];[Self.view Addgesturerecognizer:swipeleft];Swipe rightUiswipegesturerecognizer*swiperight = [[UiswipegesturerecognizerAlloc]initwithtarget:self Action: @selector (swipeaction:)];[Swiperight Setdirection:uiswipegesturerecognizerDirectionright];[Self.view Addgesturerecognizer:swiperight];Sweep upUiswipegesturerecognizer*swipetop = [[UiswipegesturerecognizerAlloc]initwithtarget:self Action: @selector (swipeaction:)];[Swipetop Setdirection:uiswipegesturerecognizerDirectionup];[Self.view Addgesturerecognizer:swipetop];Swipe downUiswipegesturerecognizer*swipedown = [[UiswipegesturerecognizerAlloc]initwithtarget:self Action: @selector (swipeaction:)];[Swipedown Setdirection:uiswipegesturerecognizerDirectiondown];[Self.view Addgesturerecognizer:swipedown];}-(void) didreceivememorywarning{[Super didreceivememorywarning];Dispose of any resources the can be recreated.}#pragma mark-swipe gesture-(void) Swipeaction: (Uiswipegesturerecognizer*) sender{NSLog (@ "%d", sender.direction);Switch (sender.direction) {Case UiswipegesturerecognizerDirectionleft:NSLog (@ "swipe left");Break Case UiswipegesturerecognizerDirectionright:NSLog (@ "swipe right");BreakCase UiswipegesturerecognizerDirectionup:NSLog (@ "swipe up");BreakCase UiswipegesturerecognizerDirectiondown: NSLog (@ "swipe down");BreakDefaultBreak}}#pragma mark-long press gesture-(void) Longpressaction: (UilongpressgesturerecognIzer *) sender{We can take advantage of the DemoView tag property, by default when tag=0If tag=0, we zoom in one more, otherwise we shrink by halfCGFloat scale;if (_demoview.tag = = 0) {Scale = 2.0;_demoview.tag = 1;} else {scale = 0.5;_demoview.tag = 0;}Sender.view.transform = Cgaffinetransformscale (sender.view.transform, scale, scale);}#pragma mark-drag-and-drop gesture-(void) Panaction: (Uipangesturerecognizer *) sender{ In the drag-and-drop, it is necessary to consider the state of the finger uigesturerecognizerstateThe state used in the drag-and-drop trend is uigesturerecognizerstateChangedUsually when using the drag-and-drop potential, when the finger leaves, should do a very small action, remind the user to drag and drop to completeif (sender.state = = UigesturerecognizerstateChanged) {Locationinview[_demoview Setcenter:[sender LocationInView:self.view];} else if (sender.state = = UigesturerecognizerstateEnded) {[_demoview Setbackgroundcolor:[uicolor Yellowcolor];}}#pragma mark-rotation gesture-(void) Rotationaction: (UirotationgesturerecogniZer *) sender{Sender.view.transform = Cgaffinetransformrotate (Sender.view.transform, sender.rotation); Similar to the kneading operation, the rotation angle also requires FangfowerSender.rotation = 0.0f;}#pragma mark-pinch gesture-(void) Pinchaction: (Uipinchgesturerecognizer*) sender{For the contents of the conversion, we will continue in the subsequent animation sectionSender.view.transform = Cgaffinetransformscale (Sender.view.transform, Sender.scale, Sender.scale);The zoom function is simple, but don't forget to reset the scale Sender.scale = 1.0f;NSLog (@ "pinch Me");}#pragma mark-tap gesture-(void) Tapaction: (UITapGestureRecognizer *) sender{NSLog (@ "Point me up%@", sender);}#pragma mark-gesture Touch event-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{NSLog (@ "Touch event! ");1. Remove the Uitouch object first2. Judging the UIView of the response click is not what we needUitouch *touch = [touches anyobject];if ([touch view] = = _imageview) {NSLog (@ "point to Image!");}} Copy code four, mobile phone shaking 1. New Shake listener View Shakelistenerview, and set Canbecomefirstresponder return yes-(BOOL) canbecomefirstresponder{return YES;} 2. In storyboard, set the Viewcontroller view class to: SHAKELISTENERVIEW3. Added in the viewcontroller.m file: Viewdidappear and viewdiddisappear become/revoke the first responder identity when the view appears and disappears 4. To increase the gesture monitoring method in the View controller:-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event{if (Event.subtype = = UieventsubtypemotionshakE) {NSLog (@ "Shake phone");}} Copy code #pragma mark-to let viewcontroller support shaking, you need to write three methods//1. Become the first responder, when the view appears, it should be the first responder-(void) Viewdidappear: (BOOL) animated{[Self.view Becomefirstresponder];Don't forget to implement the parent class method[Super viewdidappear:animated];}2. Logout of the first responder, when the view is to be closed, logoff-(void) Viewdiddisappear: (BOOL) animated{ [Self.view Resignfirstresponder];Don't forget to implement the parent class method[Super viewdiddisappear:animated];}3. Monitor and handle mobile events to determine if the phone is shaken-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event{if (motion = = UieventsubtypemotionshakE) {NSLog (@ "Shake shake, Shake to being!!!" ");}}

iOS Development Learning touch events and gesture recognition (go

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.