UIGestureRecognizer subView识别器问题 [英] UIGestureRecognizer subView recognizer problem

查看:96
本文介绍了UIGestureRecognizer subView识别器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题很难。
主要情况是这样的

The title is hard . The the main case is like this

UIView *superView = [[UIView alloc] initWithFrame:CGRectMake(0,0,400,400)];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(-200,-200,400,400)];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[subView addGestureRecognizer:tapGesture];
[superView addSubView:subView];

好的,你会发现点按这个区域时点击手势会生效(0, 0,200,200),如果你点击点(-150,-150),点击手势将不会生效。

OK , you will find that the tap gesture will take effect when you click the area in (0,0,200,200) , if you click the point (-150,-150) the tap gesture will not take effect.

我不知道superView界外的点击是否有效是否导致此问题。

I don't know whether the click outside the superView bounds to cause this problem or not.

任何人都知道如何解决此问题?

Anyone have any idea how to fix this?

推荐答案

我发现这种情况的唯一解决方法是创建一个视图实例,该视图对于作为主视图的触摸是透明的。在这种情况下,内部视图将响应触摸,因为它适合主要的边界。在我通过网络中发现的不同示例制作的课程中,我可以控制触摸可见性的级别,如下所示:
完全可见 - 所有触摸最终都在视图中。
只有子视图 - 视图本身不可见,但是子视图会触及它们。
完全看不见 - 我认为非常自我解释:)

The only workaround I've found for case like that is to create an instance of a view that is transparent for touches as main view. In such case inner view will respond to touches as it fits bounds of main. In the class I've made from different examples found in the net I can control the level of "touch visibility" like so: fully visible - all of the touches end up in the view. only subviews - the view itself invisible, but subviews get their touches. fully invisible - pretty self explanatory I think :)

我没有尝试将它与手势识别器一起使用,但我不认为会有任何问题,因为它与常规触摸完美配合。

I didn't try to use it with gesture recognizers, but I don't think there will be any problem, as it works perfectly with regular touches.

代码很简单......

The code is simple...

TransparentTouchView.h

#import <UIKit/UIKit.h>

typedef enum{
    TransparencyTypeNone = 0,         //act like usual uiview
    TransparencyTypeContent,          //only content get touches
    TransparencyTypeFull              //fully transparent for touches
}TransparencyType;

@interface TransparentTouchView : UIView {
    TransparencyType      _transparencyType;
}

@property(nonatomic,assign)TransparencyType transparencyType;

@end

TransparentTouchView.m

#import "TransparentTouchView.h"


@implementation TransparentTouchView

@synthesize 
transparencyType = _transparencyType;

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    // UIView will be "transparent" for touch events if we return NO
    switch (_transparencyType) {
        case TransparencyTypeContent:
            for(UIView* subview in self.subviews){
                CGPoint p = [subview convertPoint:point fromView:self];
                if([subview pointInside:p withEvent:event]){
                    return YES;
                }
            }
            return NO;
            break;
        case TransparencyTypeFull:
            return NO;
        default:
            break;
    }
    return YES;
}

@end

我相信你可以容纳它符合您的需求。

I believe that you can accomodate it to your needs.

这篇关于UIGestureRecognizer subView识别器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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