Resolve the gesture conflict between the right-sliding returned gesture and UIScrollView. The gesture uiscrollview

Source: Internet
Author: User

Resolve the gesture conflict between the right-sliding returned gesture and UIScrollView. The gesture uiscrollview

A page in the project displays two different functions using a horizontal Tab of scrollview, such as message and announcement functions. However, the sliding return gesture conflicts with the sliding return gesture of scrollview, as a result, the page cannot be moved back. Similarly, the Image Browsing function has also appeared.

In iOS systems, the sliding return gesture is actually a UIPanGestureRecognizer. The default operation is that UIPanGestureRecognizer takes effect only at the left of the sliding screen. UIScrollView's sliding gesture is also UIPanGestureRecognizer. When sliding the side, it makes it okay for UIScrollView to not respond to the event. First, we thought of inheriting UIScrollView to rewrite the following method, so that scrollView does not respond to the event when sliding the side. According to the responder chain, the event is finally passed to the slide gesture below.

-(UIView *) hitTest :( CGPoint) point withEvent :( UIEvent *) event {if (point. x <location. x) {// location. x is the x return nil of a certain point of the system;} else {return [super hitTest: point withEvent: event];}

However, there is a problem, that is, when different tabs on a page, you also need to slide the switch and return.

Because scrollView's sliding gesture intercepts the event, I will rewrite the proxy method of panGestureRecognizer in scrollView so that it will not intercept the event. Therefore, it inherits UIScrollView and overwrites the following method.

-(BOOL) gestureRecognizer :( optional *) gestureRecognizer updated :( optional *) else {if ([self panBack: gestureRecognizer]) {return YES;} return NO;}-(BOOL) panBack :( UIGestureRecognizer *) gestureRecognizer {if (gestureRecognizer = self. panGestureRecognizer) {Region * pan = (Region *) gestureRecognizer; CGPoint point = [pan translationInView: self]; UIGestureRecognizerState state = gestureRecognizer. state; if (UIGestureRecognizerStateBegan = state | UIGestureRecognizerStatePossible = state) {CGPoint location = [gestureRecognizer locationInView: self]; if (point. x> 0 & location. x <"set yourself" & self. contentOffset. x <= 0) {return YES ;}} return NO ;}

When you need to slide the side, panBack returns YES. At this time, I asked the scrollView gesture to coexist with the page's sliding return gesture. If the scrollView does not intercept the gesture, can I slide and return it. Okay, let's test it. you can slide the returned result, but why is scrollView moving? It affects the appearance too much. It seems that another method is needed, I went back to the idea of the first method, so that the corresponding panGesture is returned when the scrollView is switched, and the response is not returned when sliding. That is, rewrite another panGestureRecognizer proxy method in the scrollView.

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {    if ([self panBack:gestureRecognizer]) {        return NO;    }    return YES;}

Method 2:

-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer kernel :( UIGestureRecognizer *) kernel {// first, judge whether the kernel is a system pop gesture if ([kernel. view isKindOfClass: NSClassFromString (@ "UILayoutContainerView")]) {// judge whether the state of the system gesture is an or fail, and determine whether the position of the scrollView is exactly on the leftmost if. state = UIGestureRecognizerStateBegan & self. contentOffset. x = 0) {return YES;} return NO ;}

The above code is all in a custom UIScrollView. Just rewrite the above method. Then let the scrolling scrollView inherit the custom UIScrollView.

Principle:
The pan gesture of scrollView invalidates the system's pan gesture. Therefore, you only need to enable the two gestures when the system gesture fails and the position of the scrollView is in the initial position.

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.