IOS: Can I override the on / off behavior of a UIScrollView?

I draw a graph on a UIView that is contained in a UIScrollView so that the user can scroll horizontally to look around the entire graph.

Now I want to increase the graph when the user pinches with two fingers, but instead of scaling the view at the same speed for the X and Y directions, I want to scale only in the X direction, changing the X scale, without changing the Y scale.

I think I need to catch a buzzing gesture and redraw the chart, overriding the default behavior.

But is there a way to do this?

I had a very difficult time to catch a pinch gesture on a UIScrollView , as it cancels touches when it starts to scroll. I want the scaling to work even after the UIScrollView undoes the touch .: (

Thanks, Chickens

+4
source share
4 answers

Although you cannot delete an existing gesture recognizer, you can disable it and then add your own:

 // Disable existing recognizer for (UIGestureRecognizer* recognizer in [_scrollView gestureRecognizers]) { if ([recognizer isKindOfClass:[UIPinchGestureRecognizer class]]) { [recognizer setEnabled:NO]; } } // Add our own UIPinchGestureRecognizer* pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]; [_scrollView addGestureRecognizer:pinchRecognizer]; [pinchRecognizer release]; 

Then in

 - (void) pinch:(UIPinchGestureRecognizer*)recognizer { .. } 

use

 [recognizer locationOfTouch:0 inView:..] [recognizer locationOfTouch:1 inView:..] 

to find out if the user is pinching horizontally or vertically.

+11
source

Instead, you should refer to gestureRecognizers (defined in a UIView), some of them are used by scrolling,

determine which one is a recognition pinch and call removeGestureRecognizer: in scroll mode, then create your own and run it, add it using addGestureRecognizer:

all this is a public API, recognizers and in what order they are (currently), so the program is protected when accessing them

(this is a perfectly correct way to manipulate UIKit views, and Apple will not / should not have problems with it, although they do not guarantee its operation in any future version)

+5
source

You must subclass UIScrollView and override the touchhesBegan: method. Do not name [super touchhesBegan:], but instead adjust the scale as you like:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //Anything you want. Probably you would want to store all the touches //or their values, so that you can compare them to the touches //in the touchesEnded: method, //thus letting you know what the pinch amount was } 

If you like, you can judge whether this is an infringement or not, and if not, call the super method and process it yourself for custom pinches.

+1
source

The answers of Edsko and bshirley are good, but they do not say where to place the code.

At first I put it in the viewDidLoad method, but Recch Gesture Recognizer was not found in scrollview (possibly because my scrollview is an IBOutlet).

Then I tried in viewWillAppear or viewDidAppear , and the UIPinchGestureRecognizer was here.

0
source

All Articles