昨天想着写一个带有控件的弹窗,并且点击空白区域可以退出。
“弹窗”我是用hidden属性来实现的,本意是加在空白view上然后一起隐藏一起显示。
self.blankView= [[UIViewalloc]initWithFrame:self.view.frame];
self.blankView.backgroundColor = [UIColor clearColor];
self.blankView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(blanckViewClick:)];
[self.blankView addGestureRecognizer:tapGesture];
[self.viewaddSubview:self.blankView];
[self.blankView addSubview:self.collectionView];
点击事件为
-
(void)bgViewClick:(UITapGestureRecognizer*)gesture {
self.imageBgView.hidden = YES;
}
结果发现点击collectionViewCell也触发了blankView的点击事件。
网上看了一下说是检测手势的点击位置,判断在不在子view里面。看了下代码实在很麻烦,然后我想到了另一个办法。
我把collectionView设为了self.view的子view
[self.view addSubview:self.blankView];
[self.view addSubview:self.collectionView];
如果担心collectionView在blankView下面可以加上下面这句
[self.view bringSubviewToFront:self.collectionView];
并且将隐藏view的方法写在了BOOL属性 showAlert的setter里面
-
(void)setShowAlert:(BOOL)showAlert {
_showAlert= showAlert;
self.blankView.hidden=self.collectionView.hidden= !showAlert;
}
-
(void)bgViewClick:(UITapGestureRecognizer*)gesture {
self.showAlert=NO;
}
问题解决