iOS-关于一些手势冲突问题(scrollView 嵌套 tableView)
简单说下关于开发中容易遇到的父试图添加手势与子试图点击事件冲突,uiscrollview 嵌套 uiscrollview 、 uiscrollview 嵌套 uitableview的情况手势冲突问题;
点击冲突
如果给现有的基于 uiview 的 xktestview 上加一个点击手势 gesttap,然后在 xktestview 中间区域添加一个 tableview,我们想响应 gesttap,同时也想响应 tableview 的 cell 点击代理事件,这时可以添加 gesttap 点击手势代理:
<uigesturerecognizerdelegate>
然后在点击事件代理方法中实现
- (bool)gesturerecognizer:(uigesturerecognizer *)gesturerecognizer shouldreceivetouch:(uitouch *)touch { if ([nsstringfromclass([touch.view class]) isequaltostring:@"xktestview"]) { return yes; } return no; }
scrollview 嵌套 tableview 类冲突
这里直接用 scrollview 嵌套 tableview 来处理下滑动时的手势冲突问题,其实苹果并不建议我们这样做,但是在实际项目中,有些需求会经常用嵌套来实现,在什么情况下滑动 tableview 不滑动 scrollview,什么情况下滑动 scrollview 不滑动 tableview,其实如果做其他的嵌套都是一样的,先看下最终效果图:
1)首先新建一个基于 uiscrollview 的 xkbasescrollview ,并实现 <uigesturerecognizerdelegate> 代理,xkbasescrollview 用做主父试图来添加子试图内容
xkbasescrollview.h
#import <uikit/uikit.h> @interface xkbasescrollview : uiscrollview <uigesturerecognizerdelegate> @end
xkbasescrollview.m
#import "xkbasescrollview.h" @implementation xkbasescrollview //是否支持多时候触发,这里返回yes -(bool)gesturerecognizer:(uigesturerecognizer *)gesturerecognizer shouldrecognizesimultaneouslywithgesturerecognizer:(uigesturerecognizer *)othergesturerecognizer{ return yes; } @end
2)然后新建一个基于 uitableview 的 xktargettableview ,并实现 <uigesturerecognizerdelegate,uitableviewdelegate,uitableviewdatasource> 代理
xktargettableview.h
#import <uikit/uikit.h> @interface xktargettableview : uitableview ///可否滑动 @property (nonatomic,assign) bool canslide; ///滑动block通知 @property (nonatomic,copy) void (^slidedragblock)(void); @end
xktargettableview.m
#import "xktargettableview.h" @interface xktargettableview ()<uigesturerecognizerdelegate,uitableviewdelegate,uitableviewdatasource> @property (nonatomic,assign) cgfloat curroffsety; @end @implementation xktargettableview - (instancetype)initwithframe:(cgrect)frame style:(uitableviewstyle)style{ self = [super initwithframe:frame style:style]; if (self) { self.backgroundcolor = [uicolor whitecolor]; self.delegate = self; self.datasource = self; self.tablefooterview = [uiview new]; [self registerclass:[uitableviewcell class] forcellreuseidentifier:@"uitableviewcell"]; } return self; } //是否支持多时候触发,这里返回yes - (bool)gesturerecognizer:(uigesturerecognizer *)gesturerecognizer shouldrecognizesimultaneouslywithgesturerecognizer:(uigesturerecognizer *)othergesturerecognizer{ return yes; } #pragma mark ========== tableview 代理 ========== - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{ return 20; } - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath{ return 50; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"uitableviewcell"]; cell.textlabel.text = [nsstring stringwithformat:@"%ld",indexpath.row]; return cell; } #pragma mark ========== scrollview 代理 ========== - (void)scrollviewwillbegindragging:(uiscrollview *)scrollview{ _curroffsety = scrollview.contentoffset.y; } -(void)scrollviewdidscroll:(uiscrollview *)scrollview{ if (!self.canslide) { scrollview.contentoffset = cgpointmake(0, scrollview.contentoffset.y == 0 ? 0 : _curroffsety); } _curroffsety = scrollview.contentoffset.y; if (scrollview.contentoffset.y < 0 ) { self.canslide = no; scrollview.contentoffset = cgpointzero; //到顶通知父视图改变状态 if (self.slidedragblock) { self.slidedragblock(); } } scrollview.showsverticalscrollindicator = self.canslide ? yes : no; } @end
3)最后在使用的 viewcontroller 中实现
#import "viewcontroller.h" #import <sdautolayout.h> #import "xkbasescrollview.h" #import "xktargettableview.h" @interface viewcontroller ()<uiscrollviewdelegate> ///容器 @property (nonatomic,strong) xkbasescrollview *scrollview; @property (nonatomic,strong) xktargettableview *tableview; ///是否可以滑动 scrollview @property (nonatomic,assign) bool canslide; @property (nonatomic,assign) cgfloat lastpositiony; ///滑动临界范围值 @property (nonatomic,assign) cgfloat dragcriticaly; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view, typically from a nib. _dragcriticaly = 200; [self.view addsubview:self.scrollview]; self.scrollview.sd_layout. topspacetoview(self.view, 0). leftspacetoview(self.view, 0). rightspacetoview(self.view, 0). bottomspacetoview(self.view, 0); [self.scrollview setupautocontentsizewithbottomview:self.tableview bottommargin:0]; __weak __typeof__(self) weekself = self; self.tableview.slidedragblock = ^{ weekself.canslide = yes; weekself.tableview.canslide = no; }; } -(void)scrollviewdidscroll:(uiscrollview *)scrollview{ cgfloat currentpostion = scrollview.contentoffset.y; /* 当 底层滚动式图滚动到指定位置时, 停止滚动,开始滚动子视图 */ if (currentpostion >= self.dragcriticaly) { scrollview.contentoffset = cgpointmake(0, self.dragcriticaly); if (self.canslide) { self.canslide = no; self.tableview.canslide = yes; } else{ if (_lastpositiony - currentpostion > 0){ if (self.tableview.contentoffset.y > 0) { self.tableview.canslide = yes; self.canslide = no; } else{ self.tableview.canslide = no; self.canslide = yes; } } } }else{ if (!self.canslide && scrollview.contentoffset.y == self.dragcriticaly ) { scrollview.contentoffset = cgpointmake(0, self.dragcriticaly); } else{ if (self.tableview.canslide && self.tableview.contentoffset.y != 0) { scrollview.contentoffset = cgpointmake(0, self.dragcriticaly); } else{ } } } _lastpositiony = currentpostion; } - (xkbasescrollview *)scrollview{ if (!_scrollview) { _scrollview = [[xkbasescrollview alloc]init]; _scrollview.showsverticalscrollindicator = no; _scrollview.delegate = self; _scrollview.backgroundcolor = [uicolor redcolor]; uiview *view = [[uiview alloc]init]; view.backgroundcolor = [uicolor bluecolor]; [_scrollview addsubview:view]; view.sd_layout. topspacetoview(_scrollview, 0). leftspacetoview(_scrollview, 0). rightspacetoview(_scrollview, 0). heightis(300); [_scrollview addsubview:self.tableview]; self.tableview.sd_layout. topspacetoview(view, 0). leftspacetoview(_scrollview, 0). rightspacetoview(_scrollview, 0). heightis(self.view.bounds.size.height - (300 - self.dragcriticaly)); } return _scrollview; } - (xktargettableview *)tableview{ if(!_tableview){ _tableview = [[xktargettableview alloc]initwithframe:cgrectzero style:uitableviewstyleplain]; } return _tableview; } @end
注:此 demo 需引用 sdautolayout
上一篇: 支持IE6、IE7、IE8等低端浏览器的简化版vue
下一篇: vue-cli vue脚手架搭建步骤