2

是否可以在视图中实现手势识别器并将其传播到所有其他 UI 组件?如果我做这样的事情,它不起作用:

  UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
  swipe.direction = UISwipeGestureRecognizerDirectionRight;

  [self.view addGestureRecognizer:swipe];
  [TitleLabel addGestureRecognizer:swipe];
  [DescLabel addGestureRecognizer:swipe];
  [_TopView addGestureRecognizer:swipe];
  [_BottomView addGestureRecognizer:swipe];
  [_ScrollView addGestureRecognizer:swipe];
  [_TableView addGestureRecognizer:swipe];

  [swipe release];

我该怎么做?

我需要在我的视图上添加一个透明视图,覆盖所有对象?或者有一种智能的方法可以做到这一点?

4

1 回答 1

0

您的发现是正确的:多个组件的相同识别器,但是,为了完整起见......

NSInteger count = 1;
NSInteger total = [[self.view subviews] count];
for (id obj in [self.view subviews])
{
    NSLog(@"testing object %i of %i", count, total);
    count++;

    if ([obj respondsToSelector:@selector(addGestureRecognizer:)])
    {
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
        swipe.direction = UISwipeGestureRecognizerDirectionRight;
        [obj addGestureRecognizer: swipe];
        [swipe release];
        NSLog(@"swipe added");
    }
}

我预见的唯一问题是,如果您想要应用识别器的任何对象都嵌入到更多已经是self.view. 然后,您需要检查找到的子视图self.view是否属于类 type UIView,如果是则遍历该视图的子视图等。

于 2012-05-01T16:15:42.613 回答