Http://www.cnblogs.com/lexingyu/p/3702742.html
"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 Class 2-(ID) Initwithrootviewcontroller: (Uiviewcontroller *) Rootviewcontroller 3 {4 self = [super Initwithrootviewcontroller:rootviewcontroller]; 5 if (self) 6 {7 //In NAVIVC the unified processing stack in the case of whether each VC supports sliding return 8 //Current only the lowest VC off slide return 9 Self.interactivePopGestureRecognizer.delegate = (id<uigesturerecognizerdelegate>) self;10 }11 12 return self;13}
Then control the sliding return switch in the delegate.
1-(BOOL) Gesturerecognizershouldbegin: (Uigesturerecognizer *) Gesturerecognizer 2 {3 if ( Self.viewControllers.count = = 1)//Turn off the main interface right slide back 4 {5 return NO; 6 } 7 Else 8 {9 return yes;10 }11}
"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//R Eturn YES to allow both to recognize simultaneously. The default implementation returns no (by default No, gestures can be recognized simultaneously) 3//4//Note:returnin G YES guaranteed to allow simultaneous recognition. Returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture ' s delegate may return YES5-(BOO L) 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//Don't change the gestures ' dele Gates or override the getters for these properties.3 @property (nonatomic, readonly) Uipangesturerecognizer *pangesturerec Ognizer 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 and returns 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:
Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code
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 until 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 fail-(void) Requiregesturerecogni Zertofail: (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 the UINavigationController.view.gestureRecognizers to get it, As follows:
-(Uiscreenedgepangesturerecognizer *) screenedgepangesturerecognizer{ Uiscreenedgepangesturerecognizer * Screenedgepangesturerecognizer = nil; if (Self.view.gestureRecognizers.count > 0) {for (Uigesturerecognizer *recognizer in Self.view.gestureRecognizers) { if ([Recognizer Iskindofclass:[uiscreenedgepangesturerecognizer class]]) { Screenedgepangesturerecognizer = (Uiscreenedgepangesturerecognizer *) recognizer; Break ; }}} return Screenedgepangesturerecognizer;}
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.
IOS7 sliding back to coexist with scrollview two or three things