0

For UITapGestureRecognizer, you can set number of taps required to control the recognition of the UITapGestureRecognizer. If you set numberOfTapsRequired to 2 and user taps only once, then the UITapGestureRecognizer won't be triggered.

My question is How about for UIPanGestureRecognizer? How to control its recognition?

I have a view. Once I set a UIPanGestureRecognizer to it, any dragging will trigger the action. But what I want is only the dragging in X-axis. And for non-X-axis dragging, all touch events should be sent to other views underneath.

How can I do it?

THanks

Jackson Tale
  • 25,428
  • 34
  • 149
  • 271

4 Answers4

2

Set its delegate and implement

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

Then use

- (CGPoint)velocityInView:(UIView *)view;

on the gesture recogniser to calculate whether the gesture recognizer should handle it or not.

Sherman Lo
  • 2,759
  • 20
  • 20
0

Assume that your gestureRecognizer triggers the action _panRecogPanned below. You can see how the center of a subview ( the view that carries the gesture recognizer itself) moves following the transition. To disable panning on y axis, you simply set the center as the calculated new center, whereas the translation.y is omitted.

To move other subviews on the y axis, get their frame, update their origin.x property and reset the frame, they should follow your finger only on the y axis.

- (IBAction)_panRecogPanned:(id)sender{

    CGPoint translation = [_panRecog translationInView:_statementFilterView];

    //This subview only moves horizontally
    _panRecog.view.center = CGPointMake(translation.x + _panRecog.view.center.x, _panRecog.view.center.y);

    //This subview only moves vertically
    CGRect newFrame = anotherSubview.frame;
    newFrame.origin.y = anotherSubview.frame.origin.y + translation.y;
    anotherSubview.frame = newFrame;

    [_panRecog setTranslation:CGPointMake(0, 0) inView:self.view];
}
Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56
0
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:image];
[holderView addSubview:imageview];

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];

[self.view addSubview:holderView];

Raees

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
0

Check How to stop UIPanGestureRecognizer when object moved to certain frame. It may help you.

Community
  • 1
  • 1
Kiran
  • 339
  • 1
  • 14