我已经多次使用过UITapGestureRecognizer,但是在这种情况下,当点击发生时我会得到EXC_BAD_ACCESS。我认为这与我将其添加到Alert-type覆盖视图这一事实有关。由于某种原因,叠加视图即使在屏幕上也不会被保留。
我正在创建这样的视图:
HelperView *testHelper = [HelperView helperWithBodyText:@"test text"];
[testHelper presentAtPoint:screenCenter];
HelperView.m中的便捷方法如下所示:
+ (id)helperWithBodyText:(NSString*)text
{
return [[self alloc] initWithFrame:CGRectZero bodyText:text];
}
其余代码如下所示:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.container = [[AGWindowView alloc] initAndAddToKeyWindow];
self.container.supportedInterfaceOrientations = UIInterfaceOrientationMaskLandscapeRight;
self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
self.overlay.backgroundColor = [UIColor redColor];
self.overlay.alpha = 0.6;
[self.container addSubviewAndFillBounds:self.overlay]; //this fills the screen with a transparent red color, for testing
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissMe:)];
[self.overlay addGestureRecognizer:tap]; //I want to dismiss the whole view if the overlay is tapped
self.content = [[UIView alloc] initWithFrame:CGRectZero];
}
return self;
}
- (id)initWithFrame:(CGRect)frame bodyText:(NSString*)bodyText
{
self = [self initWithFrame:frame];
if (self) {
//TEST frame
self.content.frame = CGRectMake(0, 0, 217, 134);
// Initialization code
UIImage *bgImage = [[UIImage imageNamed:@"helper-bg"]
resizableImageWithCapInsets:UIEdgeInsetsMake(30, 28, 20, 20)];
UIImageView *bgImgView = [[UIImageView alloc] initWithImage:bgImage];
bgImgView.bounds = self.content.frame;
[self.content addSubview:bgImgView];
}
return self;
}
- (void)presentAtPoint:(CGPoint)loc
{
CGPoint newPoint = [self.container convertPoint:loc toView:self.container];
self.content.center = newPoint;
[self.container addSubview:self.content];
}
- (void)dismissMe:(UITapGestureRecognizer*)recognizer
{
//this never happens - I get EXC_BAD_ACCESS when I tap the overlay
}
答案 0 :(得分:1)
HelperView不是显示的视图,也不会保留,但您将其用作手势识别器的目标。 AGWindowView属性"容器"由超级视图显示和保留。你的代码需要重构,因为你有这个视图HelperView本身并没有显示任何东西,但如果你想让它像这样工作,你需要保留HelperView,这样它就不会自动释放。您可以通过将其分配给强实例变量来完成此操作。