"Reproduced please indicate the source" = = not the entire copy even if the source of the pro ...
IOS7 sliding back to coexist with scrollview two or three things
"Previously review"
Last year, I wrote this post iOS7 slide back. It is mentioned that for the application of multi-page structure, the delegate of Interactivepopgesturerecognizer can be replaced to uniformly manage the switches returned by all the pages in the application, such as in the derived class of Uinavigationcontroller
1 //I am a navigationcontroller derived class2- (ID) Initwithrootviewcontroller: (Uiviewcontroller *) Rootviewcontroller3 {4Self =[Super Initwithrootviewcontroller:rootviewcontroller];5 if(self)6 {7 //in NAVIVC, whether each VC in the stack supports sliding return8 //currently only the bottommost VC is turned off sliding back9Self.interactivepopgesturerecognizer.Delegate= (ID<UIGestureRecognizerDelegate>) Self;Ten } One A returnSelf ; -}
Then control the sliding return switch in the delegate.
1-(BOOL) Gesturerecognizershouldbegin: (Uigesturerecognizer *) Gesturerecognizer2 {3 if(Self.viewControllers.count = =1)//turn off the main interface right slide back4 {5 returnNO;6 }7 Else8 {9 returnYES;Ten } One}
"The problem Lies"
It looks pretty good .... Until I met the ScrollView.
After replacing the delegate, when used ScrollView, the left edge of the screen can not trigger the sliding back effect,
"Cause of the problem"
The sliding back is actually identified and responded to by the long-uipangesturerecognizer, which is directly related to the Uinavigationcontroller view (for convenience, The following relationships are present in Uinavigationcontroller.view, as shown below:
Uipangesturerecognizer--bind--uiscrollview
Uiscreenedgepangesturerecognizer--bind--uinavigationcontroller.view
Slide return cannot be triggered, indicating that Uiscreenedgepangesturerecognizer did not receive a gesture event.
According to Apple's official documentation, Uigesturerecognizer and UIView are many-to-one relationships (point here), and Uigesturerecognizer must be bound to view to work. It is therefore not difficult to imagine that the uigesturerecognizer is consistent with the order of reception and UIView hierarchy of gestures on the screen. Likewise, consider the example
(I am z-axis)--------------------------------------------------------------------------------------------------------- ----------------------------->
uinavigationcontroller.view-> uiviewcontroller.view-> uiscrollview-> screen and User ' s finger
That is, the Uiscrollview Pangesturerecognizer received a gesture event, directly in-place processing without passing down.
This is actually the problem of coexistence of the two Pangesturerecognizer .
"Solutions"
Apple supports multiple Uigesturerecognizer coexistence in the form of uigesturerecognizerdelegate. One of the methods is:
1 //Called when the recognition of one of Gesturerecognizer or Othergesturerecognizer would is blocked by the OTHER2 //return YES to allow both to recognize simultaneously. The default implementation returns No. by default No. ES can be recognized simultaneously)3 //4 //note:returning YES guaranteed to allow simultaneous recognition. Returning are not guaranteed to prevent simult aneous recognition, as the other gesture ' s delegate may return YES5-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer;
In a nutshell, when this method returns Yes, the gesture event is passed down, regardless of whether the current level responds to the event.
Look at Uiscrollview's header file, which is described below:
1 // Use These accessors to configure the scroll view ' s built-in gesture recognizers. 2 // The gestures ' delegates or override the getters for these properties. 3 readonly) Uipangesturerecognizer *pangesturerecognizer Ns_available_ios (5_0);
Uiscrollview itself is the delegate of its pangesturerecognizer, and Apple has made it clear that it cannot modify its delegate (there will be a warning when the change is made).
So the curve to the salvation.
Uiscrollview, as delegate, shows that the Shouldrecognizesimultaneouslywithgesturerecognizer method mentioned above is implemented in Uiscrollview , Returned the No. Create a uiscrollview category, because the same name method in the category overrides the implementation in the original. m file, allowing you to customize the delivery of gesture events, as follows:
1 @implementationUiscrollview (Allowpangestureeventpass)2 3-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer4 {5 if([Gesturerecognizer Iskindofclass:[uipangesturerecognizerclass]]6&& [Othergesturerecognizer Iskindofclass:[uiscreenedgepangesturerecognizerclass]])7 {8 returnYES;9 }Ten Else One { A returnNO; - } -}
Run the demo again to see the effect:
Well, sliding back has been successfully triggered, clap!
Wait a while, wait a while ...
It seems not quite right.
How the ScrollView also rolled!!!!!
O... It's just that the gesture event is passed down without closing the Uiscrollview response to the event.
In fact, for Uigesturerecognizer, the order in which they receive the event and the response to the event can be set separately, i.e. there is a receive chain and a response chain. The receive chain as described above, and uiview binding, is determined by the UIView hierarchy to receive the order.
The response chain, under Apple's definition, is surprisingly simple, and there is only one way to set the response relationship for multiple Gesturerecognizer:
// Create a relationship with another gesture recognizer that'll prevent this gesture's actions from being called Unti L Othergesturerecognizer transitions to uigesturerecognizerstatefailed// if Othergesturerecognizer Transitions to uigesturerecognizerstaterecognized or Uigesturerecognizerstatebegan then this recognizer would instead Transition to uigesturerecognizerstatefailed// Example usage:a single tap could require a double tap to F Ail-(void) Requiregesturerecognizertofail: (Uigesturerecognizer *) Othergesturerecognizer;
Each uigesturerecognizer is a finite state machine, and the above method will establish a dependency relationship between the two Gesturerecognizer, depending on the Gesturerecognizer.state = Failed, another gesturerecognizer can respond to gestures.
So, just need to
[_scrollview.pangesturerecognizer Requiregesturerecognizertofail:screenedgepangesturerecognizer];
Can.
Run the demo again to see the effect:
Works like a charm!!
P.S:Screenedgepangesturerecognizer is bound to Uinavigationcontroller.view, so you can traverse UINavigationController.view.gestureRecognizers to obtain, as follows:
-(Uiscreenedgepangesturerecognizer *) screenedgepangesturerecognizer{Uiscreenedgepangesturerecognizer*screenedgepangesturerecognizer =Nil; if(Self.view.gestureRecognizers.count >0) { for(Uigesturerecognizer *recognizerinchself.view.gestureRecognizers) {if([Recognizer Iskindofclass:[uiscreenedgepangesturerecognizerclass]]) {Screenedgepangesturerecognizer= (Uiscreenedgepangesturerecognizer *) recognizer; Break; } } } returnScreenedgepangesturerecognizer;}
Demo Address: Https://github.com/cDigger/CoExistOfScrollViewAndBackGesture/tree/master
Summary
Write so much, just for the first unified management sliding back a little bit of convenience, seems to be a bit more than the loss.
I do not recommend that this unconventional means be used directly in the project.
But using the bricks provided by Apple to spell out the new features in the system is one of the pleasures of iOS development.