是否可以在视图中实现手势识别器并将其传播到所有其他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];
我该怎么做?
我需要在视图上添加透明视图,其中包含所有对象? 或者有一种聪明的方法可以做到这一点?
答案 0 :(得分: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
子视图是否为类型UIView
,如果是,则迭代浏览视图的子视图等。