简体   繁体   中英

How to control the recognition of UIPanGestureRecognizer?

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

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.

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];
}
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM