前言
原理:利用cancelsTouchesInView屬性,控制點選事件優先級
I 設定tableView的點選事件優先級低于cell的選中事件
1.1 應用場景
場景1:比如篩選視圖,監聽蒙版的點選事件就隐藏篩選視圖。優先處理篩選視圖的cell選中事件。
場景2:商品清單的商品點選事件,設定低于右側的操作按鈕的點選事件
1.2 利用cancelsTouchesInView屬性進行實作
案例1:tableView的UITapGestureRecognizer的優先級低于cell的選中事件
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
[[tap rac_gestureSignal] subscribeNext:^(id x) {
@strongify(self);
if (self.alpha) {
[self.viewModel.hiddenSubject sendNext:nil];
}
}];
[self addGestureRecognizer:tap];
UITapGestureRecognizer *cutTap = [[UITapGestureRecognizer alloc] init];
cutTap.cancelsTouchesInView = NO;// 設定tableView的點選事件優先級,低于cell的選中事件
[[cutTap rac_gestureSignal] subscribeNext:^(id x) {
// @strongify(self);
[self.viewModel.hiddenSubject sendNext:nil];
}];
[self.tableView addGestureRecognizer:cutTap];
案例2:cell的選中事件低于按鈕的點選事件
self.contentView.backgroundColor = kViewColor;
UITapGestureRecognizer *cutTap = [[UITapGestureRecognizer alloc] init];
cutTap.cancelsTouchesInView = NO;// 設定V的點選事件優先級,低于cell的選中事件
[[cutTap rac_gestureSignal] subscribeNext:^(id x) {
NSLog(@" cutTap 點選了cell ");
if ( self.models.block) {
self.models.block(self.models);
}
}];
[self.contentView addGestureRecognizer:cutTap];
按鈕的點選事件推薦也用
addGestureRecognizer:
實作。
1.3 其他方式控制事件的傳遞
-
UIview的 hitTest: withEvent:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if (point.y<0) {
return [super hitTest:point withEvent:event];
}
return self;
}
- 交給子視圖處理事件
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
CGPoint redBtnPoint = [self convertPoint:point toView:self.contentView];
if ( CGRectContainsPoint(_imgViewdel.frame, redBtnPoint) ) {
return _imgViewdel;
}
// _imgView
return [super
- gestureRecognizer代理方式
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([touch.view isKindOfClass:[UITextField class]])
{
return NO;
}
// 若為UITableViewCellContentView(即點選了tableViewCell),則不截獲Touch事件
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
return NO;
}
return YES;
}
II、see aslo
iOS Horizontal Popup View 【 水準方向彈出菜單視圖】例子:商品清單支援彈出菜單進行
下/上架商品、列印商品價簽、編輯商品資訊、同步網店
等操作popover
demo 設定兩個測試開關 :
測試開關1:将水準方向彈出菜單視圖內建到cell
測試開關2:将水準方向彈出菜單視圖內建到VC的View
demo2的内容是:将水準方向彈出菜單視圖內建到VC的View
疑問解答,請關注公衆号:iOS逆向
1.支援展開折疊的彈出菜單的實作思路:
1.1将彈出視圖添加到keyWindow,蒙版也添加到主視窗(主要原因是點選螢幕的空白處,需要隐藏彈出視圖)
1.2展示的時候,動畫從右上角往左下腳延伸;隐藏的時候,動畫從左下腳往右上角收回 (展示的時候,從上往下,即x,y 慢慢變大)