我问这里(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以获得空白全屏时,我必须隐藏它。
在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]的子视图时有效吗?
答案 0 :(得分:0)
在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