Can I pass a pinch gesture recognizer to a ScrollV

2019-09-06 18:25发布

问题:

I wrote a custom pinch gesture recognizer for my ScrollView's zooming, and I handle pinch touches in a method in my view controller.

But in some cases, I want the ScrollView to behave exactly as it would normally behave (if it used the standard pinch gesture recognizer that comes with ScrollView).

Is there some way that in such cases I can pass my custom gesture recognizer to the ScrollView and it would use that recognizer to scale the view as it normally would?

Could I pass the gesture recognizer to the selector that is triggered by pinch actions?

回答1:

I have not tried this, but here's an idea:

When you set your own gestureRecognizer, store the ScrollView's pinchGestureRecognizer in an iVar. You should then be able to dynamically exchange your gestureRecognizer and the original one.



回答2:

As of iOS 5 UIScrollView exposes a pinchGestureRecognizer property, but this is read-only. To get it to use your own instead you have to subclass UIScrollView and overwrite this property hoping that internally UIScrollView also uses this property to get the gesture recognizer to add when zooming is enabled.

However it is quite dangerous messing with scroll view's gesture recognizers, especially to modify their behavior. Even just having a different delegate will trigger an exception. So proceed at your own risk.