【发布时间】: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