转发UIGesture观看背后的观点 [英] Forwarding UIGesture to views behind

查看:89
本文介绍了转发UIGesture观看背后的观点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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).

基本上我有一个superView和兄弟子视图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.

现在我的要求就是这个。

Now my requirement is this.


  1. SubView B应该接收所有滑动和点按(单和双)
    手势。

  2. SubView A应该接收所有捏合手势。

  1. SubView B should receive all swipe and tap (single and double) gestures.
  2. SubView A should receive all pinch gestures.

这是怎么回事我在视图中添加了手势识别器

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实际上是第三方库的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观看背后的观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆