Interception of touch events and interception of touch events

Source: Internet
Author: User

Interception of touch events and interception of touch events
Case: Three stacked images respond to the gesture of the corresponding view through event interception in the overlapping part.

 

Uiview does not accept three cases of event processing:

Note: For a view created through storyBoard or xib, The initwithFrame method will not be executed. You need to use-(void) awakeFromNib;

Instantiate a view of three colors:
 1 - (void)awakeFromNib 2 { 3      4         RedView *view1 = [[RedView alloc]initWithFrame:CGRectMake(20, 210, 280, 40)]; 5         [self addSubview:view1]; 6         self.redView = view1; 7          8         BlueView *view2 = [[BlueView alloc]initWithFrame:CGRectMake(60, 130, 200, 200)]; 9         [self addSubview:view2];10         [view2 setAlpha:0.5];11         self.blueView = view2;12         13         GreenView *view3 = [[GreenView alloc]initWithFrame:CGRectMake(80, 150, 160, 160)];14         [self addSubview:view3];15         [view3 setAlpha:0.5];16         self.greenView = view3;17 18 }

 

You can use either of the following methods to intercept a gesture:

-(UIView *) hitTest :( CGPoint) point withEvent :( UIEvent *) event;
-(BOOL) pointInside :( CGPoint) point withEvent :( UIEvent *) event;

Override the hittext method to intercept the order of users' touch views
The hitTest method is triggered by window.
If you want the user to press the screen, immediately make a response using touchesBegin
If you want the user to leave the screen, immediately respond, using touchesEnd
Generally, touchesBegin is used to prevent the user from believing that the click is unresponsive.

Convert the hitTest point to the redView point and use convertPoint: toView;

CGPoint redP = [self convertPoint: point toView: self. redView];

Determine whether a point is inside the View:

If ([self. greenView pointInside: greenP withEvent: event]) {
Return self. greenView;
}

HitTest interception time code:
1-(UIView *) hitTest :( CGPoint) point withEvent :( UIEvent *) event 2 {3 // 1. determine whether the current view can accept the user response 4/* self. userInteractionEnabled = YES 5 self. alpha> 0.01; 6 self. hidden = no; 7 */8 // 2. traverse all the sub-views and determine whether to respond to user touch. 9 // 3. send the event to the upper-level view controller for processing 10 // 4. return nil; If nil is returned, the current view and its subviews do not respond to user touch. 11/* 12 parameter description: 13 point: the parameter is the point of the user's touch position relative to the current view coordinate system. 14. Watching: The following two are used together, use recursion to determine the subview 15-(UIView *) hitTest :( CGPoint) point withEvent :( UIEvent *) event; 16-(BOOL) pointInside :( CGPoint) point withEvent :( UIEvent *) event; 17 these two methods are only used to intercept touch events. They interrupt the responder chain and do not call them at ordinary times. 18 reminder: If there is no last resort, it is best not to rewrite the hitTest method by yourself; 19 */20 CGPoint redP = [self convertPoint: point toView: self. redView]; 21 // point 22 CGPoint greenP = [self convertPoint: point toView: self. greenView]; 23 // pointInside uses the coordinate points in the specified view to determine whether the view is inside. It is best not to use them in daily development. 24 if ([self. greenView pointInside: greenP withEvent: event]) {25 return self. greenView; 26} 27 NSLog (@ "% @", NSStringFromCGPoint (redP); 28 if ([self. redView pointInside: redP withEvent: event]) {30 return self. redView; 31} 33 return [super hitTest: point withEvent: event]; 34}

Code in: https://github.com/zhangjinling/IOSProgects/tree/master/%E6%89%8B%E5%8A%BF/03.%E8%A7%A6%E6%91%B8%E4%BA%8B%E4%BB%B6%E6%8B%A6%E6%88%AA

Related Article

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.