使用2个UIPanGestures处理2个视图。

时间:2018-07-27 16:59:14

标签: ios

我有一个附加了UIPanGestureRecognizer(PAN_A)的视图(A),第二个视图(B)是A的 subview ,并且它也拥有自己的UIPanGestureRecognizer(PAN_B)。视图A是PAN_A的代表,而视图B是PAN_B的代表

我很难使它们正常工作。

我希望当B的#!/usr/bin/env bash set -o nullglob # make globs expand to nothing, not themselves, when no matches found dirs=( "$1"*/ ) # list directories starting with $1 into an array # Validate that our glob actually had at least one match (( ${#dirs[@]} )) || { printf 'No directories start with %q at all\n' "$1" >&2; exit 1; } idx=$(( RANDOM % ${#dirs[@]} )) # pick a random index into our array echo "${dirs[$idx]}" # and look up what's at that index 返回gestureRecognizerShouldBegin时,该事件会自动传递到A并由PAN_A识别...但这完全没有发生:/

当我需要在B上启用手势时,我还尝试了false的任意组合以将其禁用。手势似乎总是被PAN_B困住

根据某些条件,我需要让PAN_A识别和处理手势,或者由PAN_B识别和处理手势。我希望有一种开关可以在视图B关闭时关闭视图B的识别。不需要。

如果您需要以不同的方式处理同一层次结构上的触摸,哪个是最好的方法?是否应在需要时在ROOT级别处理所有手势(在我的示例中,使用A视图的PAN_A)并将手势信息“手动”传递给B?

2 个答案:

答案 0 :(得分:0)

也许使用基于手势识别的委托这样的东西:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
//Maybe extra validation
if ([touch.view isDescendantOfView:yourSubView]) {
    return NO;
}
    return YES;
}

或者您可以在验证后使用firstResponder方法viewA.firstResponder处理

答案 1 :(得分:0)

除了aViewbViewpanApanB的委托人之外,您可以尝试同时将panApanB当作委托人吗他们的通用视图控制器?这样,手势识别器委托方法将同时由两个平移手势触发,并且将按您期望的方式工作。

以下是一些对我有用的示例代码:(在此示例中,手势识别器和视图的所有连接均由情节提要完成,但是ViewControllerpanApanB。)panAActive是您想要的“开关”。如果将其翻转,panApanB将交替工作。

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *aView;
@property (weak, nonatomic) IBOutlet UIView *bView;

@property (weak, nonatomic) IBOutlet UIPanGestureRecognizer *panA;
@property (weak, nonatomic) IBOutlet UIPanGestureRecognizer *panB;

@property BOOL panAActive;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.panAActive = NO;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer == self.panA) {
        return self.panAActive;
    }
    if (gestureRecognizer == self.panB) {
        return !self.panAActive;
    }
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ((gestureRecognizer == self.panA && otherGestureRecognizer == self.panB) ||
        (gestureRecognizer == self.panB && otherGestureRecognizer == self.panA)){
        return YES;
    }
    return NO;
}

- (IBAction)aPanned:(id)sender {
    NSLog(@"aPanned");
}

- (IBAction)bPanned:(id)sender {
    NSLog(@"bPanned");
}