【问题标题】:Why doesn't UITapGestureRecognizer work in subview of keyWindow (full screen)?为什么 UITapGestureRecognizer 在 keyWindow 的子视图(全屏)中不起作用?
【发布时间】:2018-08-14 13:28:31
【问题描述】:

我在这里问过 (UIView added as an UIWindow subview doesn't respond to taps),但我认为它应该有自己的问题。

如果我以这种方式设置子类 UIView (CustomView),我无法让手势在视图中工作:

CustomView *customView = [[[CustomView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.frame];

customView.frame = [UIApplication sharedApplication].keyWindow.bounds; // leaves space at bottom of screen if not here

[[UIApplication sharedApplication].keyWindow addSubview:customView];

但如果我这样设置,手势可以正常工作:

CustomView *customView = [[[CustomView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.frame];

customView.frame = [UIApplication sharedApplication].keyWindow.bounds; // leaves space at bottom of screen if not here

[[UIApplication sharedApplication].windows[0] addSubview:customView];

请注意,使用 .windows[0] 而不是 .keyWindow 意味着状态栏仍然存在,所以当我取消隐藏 customView 以获得空的全屏时,我必须隐藏它。

在自定义视图中:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    id mainView;
    if (self)
    {
        NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"CustomNib" owner:self options:nil];
        mainView = [subviewArray objectAtIndex:0];
    }
    return mainView;
}

这是手势设置,也在 customView 中:

UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self addGestureRecognizer:singleFingerTap];

任何想法为什么手势识别器在视图是 .keyWindow 的子视图时不起作用但在视图是 .windows[0] 的子视图时起作用?

【问题讨论】:

    标签: ios uiview uigesturerecognizer


    【解决方案1】:

    initWithFrame 中,您返回的是UIView --- 不是 CustomView 的实例。

    您可能希望您的CustomView 代码如下所示:

    #import "CustomView.h"
    
    @implementation CustomView
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
    
        UIView *mainView;
        if (self)
        {
            NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"CustomNib" owner:self options:nil];
            mainView = [subviewArray objectAtIndex:0];
            mainView.frame = self.bounds;
            mainView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            [self addSubview:mainView];
            [self setupTap];
        }
        return self;
    }
    
    - (void) setupTap {
    
        UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        [self addGestureRecognizer:singleFingerTap];
    
    }
    
    - (void)handleTap:(UIGestureRecognizer *)g {
        NSLog(@"Tap in CustomView!");
    }
    
    @end
    

    【讨论】:

    • 谢谢@DonMag。不幸的是,这并没有解决问题。
    • 我应该提一下,添加到 [UIApplication sharedApplication].keyWindow 的子视图内部没有任何子视图,但它立即开始通过 [self.layer addAnimation:self.animation forKey: 播放 CAKeyframeAnimation: @“背景颜色”];如果将子视图添加到 [UIApplication sharedApplication].windows[0],它的手势工作正常。
    • 那么您可能需要一个子视图吗?如果您发布完整的CustomView 我会看看。
    猜你喜欢
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    相关资源
    最近更新 更多