问题描述
我正在开发一个 iphone(iOS 4.0 或更高版本)应用程序,并且在多个视图之间的触摸处理方面遇到了一些问题.我有这样的视图结构
I am working on an iphone (iOS 4.0 or later) app and having some troubles with touch handling between multiple views. I am having a view structure like this
---> A superView
|
---> SubView - A
|
---> SubView - B (exactly on top of A, completely blocking A).
基本上我有一个超级视图,以及兄弟子视图 A 和 B.B 与 A 具有相同的框架,因此完全隐藏了 A.
Basically I have a superView, and sibling subviews A and B. B has the same frame as A, hence hiding A completely.
现在我的要求是这个.
- SubView B 应该接收所有滑动和点击(单次和双次)手势.
- 子视图 A 应接收所有捏合手势.
这就是我向视图添加手势识别器的方式
This is how I added gesture recognizers to the views
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe:)];
[leftSwipe setDirection:(UISwipeGestureRecognizerDirectionLeft)];
leftSwipe.delegate = self;
[bView addGestureRecognizer:leftSwipe];
[leftSwipe release];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe:)];
[rightSwipe setDirection:(UISwipeGestureRecognizerDirectionRight)];
rightSwipe.delegate = self;
[bView addGestureRecognizer:rightSwipe];
[rightSwipe release];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
pinch.delegate = self;
[aView addGestureRecognizer:pinch];
[pinch release];
我做了一些研究,对我来说 UIGestureRecognizerDelegate
看起来很有希望,我实现了委托方法
I did some research and to me UIGestureRecognizerDelegate
looked promising and I implemented the delegate method
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
我为 SubView B 返回了 NO,希望底层视图能够获得这些事件.虽然没有这样的运气
I returned NO for SubView B, hoping that underlying view will get these event. No such luck though
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]] && [gestureRecognizer view] == bView) {
NSLog(@"pinchGesture");
//bView.userInteractionEnabled = NO;
return NO;
}
return YES;
}
然后我在委托回调中禁用了 SubView B 的用户交互(上面块中的注释代码),当识别出捏合手势时,希望 SubView A 接收手势的剩余部分.那里也没有这样的运气..
Then I disabled user interaction of SubView B inside the delegate callback (commented code in above block), when pinch gesture is recognized, hoping remaining part of gesture will be received by SubView A. No such luck there too..
这就是我现在的立场.我看过这个问题,其中接受的答案涉及属性 cancelsTouchesInView
.但我相信 cancelsTouchesInView
只会取消特定的触摸事件,不会转发它.
So that is where I stand now. I have seen this question, where the accepted answer involves property cancelsTouchesInView
. But I believe cancelsTouchesInView
only cancel a particular touch event, do not forward it.
还有其他方法可以达到我的要求吗?我准备好处理您提供的任何提示.
Any other way to achieve my requirement? I am ready to work on whatever hint you provide.
我所谓的 subView A 实际上是一个 3rd 方库的 View 类的一个实例,它带走了所有的触摸,我无法控制它的任何手势.我想要左右滑动的不同实现,我想要捏合、点击等工作,就像它与这个第三方视图一起工作一样.所以我在 A (SubView B) 的顶部放置了一个视图来左右滑动.但现在我想将其他手势事件转发到底层库.
My so called subView A is actually an instance of a 3rd party library's View class, which takes all touches away and I don't have any control over any gestures on it. I want different implementation for left and right swipe and I want pinch, tap etc work just like it is working with this third party view. So I put a view on top of A (SubView B) to get left and right swipes. But now I want to forward other gesture events to underlying library.
推荐答案
如果我不明白你的问题是正确的,你可以添加另一个带有 rect 的清晰视图,与 A 和 B 视图相同,并在其上实现所有手势:当你做捏手势时,控制子视图 A,当滑动和点击(单次和双次)手势时 - 控制子视图 B.你可以用不同的方式来做:通过指针或只是将接收到的手势发送到类中的方法,控制你的子视图.
If i undestand your problem correct, you may just add another, clear view with rect, same as you A and B view, and implement all gesture on it: when you do pinch gesture, control subView A, when swipe and tap (single and double) gestures - control subView B. You can do it different ways: via pointers or just sending recived gesture to method in class, wich controls your sub view.
例如:
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe:)];
[leftSwipe setDirection:(UISwipeGestureRecognizerDirectionLeft)];
leftSwipe.delegate = subViewAcontroller;
[clearView addGestureRecognizer:leftSwipe];
[leftSwipe release];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe:)];
[rightSwipe setDirection:(UISwipeGestureRecognizerDirectionRight)];
rightSwipe.delegate = subViewAcontroller;
[clearView addGestureRecognizer:rightSwipe];
[rightSwipe release];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
pinch.delegate = subViewBcontroller;
[clearView addGestureRecognizer:pinch];
[pinch release];
或:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
NSLog(@"pinchGesture");
[subViewBcontroller solvePinchGesture: gestureRecognizer];
}
//etc
return YES;
}
这篇关于将 UIGesture 转发到后面的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!