IOS input event touch event Gesture Recognition mobile phone shake 1. iOS input event touch event (slide, click) motion event (shake, mobile phone tilt, walking ), remote control events that do not require human intervention (headset control mobile phone sound) 1⃣When iOS event objects are all instances of the UIEvent class, the UIEvent class defines the enum constant for the event type: typedef NS_ENUM (NSInteger, UIEventType) {UIEventTypeTouches, UIEventTypeMotion, UIEventRemoteControl ,}; the Touch event must inherit the UIResponser. 2. The touch Event 1⃣️ UIView, which has four methods to process different touch events. UIView is a subclass of UIResponder. You can overwrite the following four methods to process different touch events. 1. one or more fingers start to touch the screen-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event2. one or more fingers move on the screen (as your fingers move, will continuously call this method)-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event3. one or more fingers leave the screen-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event4. before the touch ends, a system event (for example, call in) interrupts the touch process-(void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event replication 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 mobile-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {NSLog (@ "Number of Touch mobile Touch objects: % d", [touches count]); // view of the yellow color on the UI to be moved // 1. obtain the position UITouch * touch = [touches anyObject]; CGPoint location = [touch locationInView: self. view]; // 2. obtain the last finger position CGPoint preLocation = [touch previuslocationinview: self. view]; // 3. calculates the offset CGPoint offset = CGPointMake (location. x-preLocation. x, location. y-preLocation. y); // 4. use the calculated offset to adjust the position of the view [_ demoView setCenter: CGPointMake (_ demoView. center. x + offset. x, _ demoView. center. y + offset. y)]; // complete UITouch event debugging method NSLog (@ "touch mobile"); for (UITouch * touch in touches) {NSLog (@ "% @", touch) ;}# pragma mark touch end-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {// complete UITouch event debugging method NSLog (@ "touch done"); for (UITouch * touch in touches) {NSLog (@ "% @", touch) ;}# pragma mark touch interrupt-(void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event {// complete UITouch event debugging method NSLog (@ "touch interrupt"); for (UITouch * touch in touches) {NSLog (@ "% @", touch) ;}} copy Code 2⃣If the hit-test view cannot process the event, it is passed up through the responder chain. if the controller of the hit-test view exists, it is passed to the Controller; if the Controller does not exist, it is passed to its parent view 2. if the view or its controller cannot process the received events or messages, it is passed to the parent view 3 of the view. if the upper-layer view in the view inheritance tree cannot process received events or messages, repeat steps 1, 24. in the top-level view of the view inheritance tree, if the received event or message cannot be processed, the event or message is transmitted to the window object for processing. if the window object cannot be processed, it will pass the event or message to the UIApplication object 6. if the UIApplication cannot process the event or message, it will be discarded. When the user clicks the screen, a UITouch object will be generated and passed to the UIApplication, then, the window is responsible for finding the view object (hitTest, pointInside) that is most suitable for the corresponding Touch event. After finding the right view, the Touch method is completed by the corresponding view, and the upper view no longer takes over 3⃣The consumer does not accept three methods for event processing and does not receive user interaction: userInteractionEnabled = NO; hide: hidden = YES; Transparency: alpha = 0 ~ 0.01 3. Gesture Recognition 1⃣Currently, Alibaba iOS supports Gesture Recognition (6 types) Click Merge (click) Merge (pinch) UIPanGestureRecognizer (drag) rotate (Light Scan) UIRotationGestureRecognizer (rotate) UILongPressGestureRecognizer (long press) 2⃣How to Use NLP Gesture Recognition (step 4) is usually defined during view loading (UIGestureRecognizer is an abstract class and needs to be instantiated and used) create a gesture recognition instance to set Gesture Recognition attributes, such as the number of fingers, add gesture recognition to a specified view, and write a gesture trigger response method 3.⃣Gesture Recognition Status (7) 1. // no touch event occurs. The default status of all Gesture Recognition is changed. // when a gesture has started but has not been changed or is completed, the UIGestureRecognizerStateBegan status changes to UIGestureRecognizerStateChanged, // complete UIGestureRecognizerStateEnded with a gesture. // The gesture is canceled and restored to the Possible State failed. // The gesture fails to be restored to the Possible State failed. // The gesture recognition token = UIGestureRecognizerStateEnded. gesture State of the recognized attribute -- gesture Status view -- common method of gesture generation view locationInView to obtain the location where the gesture occurs copy the code-(void) viewDidLoad {[super viewDidLoad];/** 1. demonstration point by gesture * // according to the instantiation method, we know: // 1. there is a message processing object, which should be self // 2. we need to define a method. When the gesture recognition detects, run UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (tapAction :)]; // setNumberOfTapsRequired: Times [tap setNumberOfTapsRequired: 1]; // setNumberOfTouchesRequir The number of fingers by the ed point [tap setNumberOfTouchesRequired: 1]; // adds gesture recognition to the view [self. demoView addGestureRecognizer: tap];/** 2. pinch points by gesture */UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget: self action: @ selector (pinchAction :)]; [self. demoView addGestureRecognizer: pinch];/** 3. rotate point by gesture */UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget: self ac Tion: @ selector (rotationAction :)]; [self. demoView addGestureRecognizer: rotation];/** 4. drag and Drop by gesture */UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panAction :)]; [self. demoView addGestureRecognizer: pan];/** 5. long-press gesture */UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (longPressAction :) ]; [Self. demoView addGestureRecognizer: longPress];/** 6. you need to specify the direction of the light scanning gesture. If you do not specify the direction, then you can only receive a light scan event to the right * // sweep the event to the left * swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (swipeAction :)]; [swipeLeft setDirection: UISwipeGestureRecognizerDirectionLeft]; [self. view addGestureRecognizer: swipeLeft]; // scan UISwipeGestureRecognizer * swipeRight = [UISw IpeGestureRecognizer alloc] initWithTarget: self action: @ selector (swipeAction :)]; [swipeRight setDirection: uiswipegesturerecognizerdireright]; [self. view addGestureRecognizer: swipeRight]; // sweep upstream * swipeTop = [[delealloc] initWithTarget: self action: @ selector (swipeAction :)]; [swipeTop setDirection: direction]; [self. view addGest UreRecognizer: swipeTop]; // swuiswipegesturerecognizer * swipeDown = [[delealloc] initWithTarget: self action: @ selector (swipeAction :)]; [swipeDown setDirection: callback]; [self. view addGestureRecognizer: swipeDown];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} # pragma mark-sweep Gesture-(void) swipeAction :( UISwipeGestureRecognizer *) sender {NSLog (@ "% d", sender. direction); switch (sender. direction) {case uiswipegesturerecognizerdireleft: NSLog (@ ""); break; case uiswipegesturerecognizerdireright: NSLog (@ ""); break; case when: NSLog (@ "scan up"); break; case UISwipeGestureRecognizerDirectionDown: NSLog (@ "scan down"); break; default: break ;}} # Pragma mark-Long-pressed gesture-(void) longPressAction :( UILongPressGestureRecognizer *) sender {// we can use the Tag attribute of demoView. By default, tag = 0 // If tag = 0, otherwise, we will scale down CGFloat scale by half; 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 :( UIPanGestureReco Gnizer *) sender {// UIGestureRecognizerState In the drag-and-drop gesture, which is the UIGestureRecognizerStateChanged state in the drag-and-drop gesture. // when using the drag-and-drop gesture, when the finger leaves, a very small action should be performed to remind the user to drag and drop to complete if (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 must also be fang fuwei sender. rotation = 0.0f ;}# pragma mark-Pinch gesture-(void) pinchAction :( UIPinchGestureRecognizer *) sender {// For the conversion content, we will continue sender in the subsequent animation part. view. transform = CGAffineTransformScale (sender. view. transform, sender. scale, Sender. scale); // The scaling function is simple, but do not forget to reset the ratio to sender. scale = 1.0f; NSLog (@ "pinch me") ;}# pragma mark-point by gesture-(void) tapAction :( UITapGestureRecognizer *) sender {/** in the development process, if there is no need, it is best not to use a UI control, both touch and gesture. */NSLog (@ "Click me % @", sender) ;}# pragma mark-gesture touch event-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {NSLog (@ "Touch event! "); // 1. retrieve the UITouch object first // 2. determine whether the UIView for the response is the UITouch * touch = [touches anyObject]; if ([touch view] ==_ imageView) {NSLog (@ "indicates the image! ") ;}} Copy Code 4. Shake the phone 1. create a new ShakeListenerView and set canBecomeFirstResponder to return YES-(BOOL) canBecomeFirstResponder {return YES;} 2. in the Storyboard, set the Class of ViewController's View to: ShakeListenerView3. in ViewController. add viewDidAppear and viewDidDisappear to/from the m file when the view appears or disappears. 4. add the gesture listening method-(void) motionBegan :( UIEventSubtype) motion withEvent :( UIEvent *) event {if (event. subtype = UIEventSubtypeMotionShake ){ NSLog (@ "shake phone") ;}} copy the Code # pragma mark-to make ViewController support shaking, write three methods // 1. as the first responder, when a view appears, it should become the first responder-(void) viewDidAppear :( BOOL) animated {[self. view becomeFirstResponder]; // do not forget to implement the parent class method [super viewDidAppear: animated];} // 2. log out of the first responder. When the view is closed, log out-(void) viewDidDisappear :( BOOL) animated {[self. view resignFirstResponder]; // do not forget to implement the parent class method [super viewDidDisappear: animated];} // 3. listen to and handle mobile events, and determine if the mobile phone is shaken- (Void) motionBegan :( UIEventSubtype) motion withEvent :( UIEvent *) event {if (motion = UIEventSubtypeMotionShake) {NSLog (@ "Shake, shake to Grandma !!! ");}}