Uiview only collects move tracks and does not receive "click" or "Long press" events.

Source: Internet
Author: User

Refer:

[1] http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MultitouchEvents/MultitouchEvents.html#//apple_ref/doc/uid/TP40009541-CH3-SW20

Scenario:

If you have a full-screen and transparent uiview built on the current interface, this uivew is not displayed, but only collects the point when the user moves the screen. However, to allow users to respond to the view behind the full-screen view after clicking and long-time, the full-screen view does not exist.

Solution:

Solution 1

Set the userinteractionenabled of the full-screen and transparent view to No. Capture the sendevent of the uiapplication and record all vertices in the move operation. At this time, all the touch events of the user are executed according to the original, the full screen view really does not exist.

The core implementation of objective-C Runtime code is as follows:

    //Swap the implementations of our interceptor and the original sendEvent:    Method oldMethod = class_getInstanceMethod(self, @selector(sendEvent:));    Method newMethod = class_getInstanceMethod(self, @selector(interceptSendEvent:));    method_exchangeImplementations(oldMethod, newMethod);

To put it simply, create a category of uiapplication and replace sendevent with method_exchangeimplementations. When the system calls sendevent, it first calls our own defined method and then records the point of the move, finally, call the system's original sendevent method.

Solution 2

Set the userinteractionenabled of the full screen and transparent view to yes, and override the four event handling methods of the parent class.

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

- (void)forwardTouchBegan:(id)obj{NSArray* array = (NSArray*)obj;NSSet*   touches = [array objectAtIndex:0];UIEvent* event   = [array objectAtIndex:1];[[self nextResponder] touchesBegan:touches withEvent:event];}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{touchMove_ = NO;[self performSelector:@selector(forwardTouchBegan:) withObject:[NSArray arrayWithObjects:touches,event,nil] afterDelay:0.5];}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{touchMove_ = YES;[[self class] cancelPreviousPerformRequestsWithTarget:self];}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{if (!touchMove_){[[self nextResponder] touchesEnded:touches withEvent:event];}}

These two methods are mainly used here. forwardtouchbegan is called in a delayed manner during begin, and the call is canceled during move. Here, the latency method is used to effectively solve the problem that the user cannot determine whether the user will move after touch in. Refer to [1] to list the methods used in the Apple documentation.
Click or double-click, but the performselector is called at the touch end startup delay. When touch begin is started, the call is canceled based on tapcount.

-(Void) specify mselector :( SEL) aselector withobject :( ID) anargument afterdelay :( nstimeinterval) delay;

+ (Void) cancelpreviousperformrequestswithtarget :( ID) atarget;

However, solution 2 is related to the background view and the full-screen viewview hierarchy. If two windows are different, the background view may still not receive the event through [self nextresponder.

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.