简体   繁体   English

如何控制UIPanGestureRecognizer的识别?

[英]How to control the recognition of UIPanGestureRecognizer?

For UITapGestureRecognizer, you can set number of taps required to control the recognition of the UITapGestureRecognizer. 对于UITapGestureRecognizer,您可以设置控制UITapGestureRecognizer识别所需的抽头数。 If you set numberOfTapsRequired to 2 and user taps only once, then the UITapGestureRecognizer won't be triggered. 如果将numberOfTapsRequired设置为2并且用户只点击一次,则不会触发UITapGestureRecognizer。

My question is How about for UIPanGestureRecognizer? 我的问题是UIPanGestureRecognizer怎么样? How to control its recognition? 如何控制其认可?

I have a view. 我有一个观点。 Once I set a UIPanGestureRecognizer to it, any dragging will trigger the action. 一旦我为它设置了UIPanGestureRecognizer,任何拖动都会触发该动作。 But what I want is only the dragging in X-axis. 但我想要的只是在X轴上拖动。 And for non-X-axis dragging, all touch events should be sent to other views underneath. 对于非X轴拖动,应将所有触摸事件发送到下面的其他视图。

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. 假设您的gestureRecognizer触发下面的_panRecogPanned操作。 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. 要禁用y轴上的平移,只需将中心设置为计算的新中心,而省略translation.y。

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. 要在y轴上移动其他子视图,获取其框架,更新其origin.x属性并重置框架,它们应仅在y轴上跟随您的手指。

- (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 Raees

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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