我在视图中有两个子视图。框架是平等的。视图具有向左滑动手势识别器,B视图具有向右滑动手势识别器。 B在A后面,所以我只能触发向左滑动手势。我怎么能同时收到?
答案 0 :(得分:2)
UIGestureRecognizerDelegate
并处理shouldRecognizeSimultaneouslyWithGestureRecognizer
。您的代表:
// leftGesture
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer
{
return otherGestureRecognizer == rightGesture;
}
答案 1 :(得分:1)
我怎样才能同时收到?
将两个手势识别器添加到视图A,因为这是前面的视图。由于您需要通过视图B处理正确的滑动手势,因此将视图B设为右滑动手势识别器的目标。
更好的选择可能是让您的视图控制器成为两个手势识别器的目标,并让它理清如何处理用户的手势。这有助于从了解如何设置用户界面的业务中获取视图 - 他们只需要遵循视图控制器的指令。如果您采用该计划,您的视图控制器可能会部分显示:
- (void)viewDidLoad
{
UISwipeGestureController *leftSwipe = [[UISwipeGestureController alloc] initWithTarget:self action:@selector(leftSwipe:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
UISwipeGestureController *rightSwipe = [[UISwipeGestureController alloc] initWithTarget:self action:@selector(rightSwipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.viewA addGestureRecognizer:leftSwipe];
[self.viewA addGestureRecognizer:rightSwipe];
}
- (IBAction)leftSwipe:(id)sender
{
[self.viewA doSomething];
}
- (IBAction)rightSwipe:(id)sender
{
[self.viewB doSomethingElse];
}