服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS中UIScrollView嵌套UITableView的实践教程

iOS中UIScrollView嵌套UITableView的实践教程

2021-03-15 15:58maxZhang IOS

在UIScrollView嵌套UITableView的问题相信大家都遇到过,小编最近在工作中就遇到了这个问题,所以这篇文章主要介绍了iOS中UIScrollView嵌套UITableView的相关资料,文中介绍的方法是通过自己的实践所得来的,需要的朋友可以参考借鉴,下

前言

最近因为工作项目中需要用到uiscrollview嵌套uitableview嵌套交互问题,顺便网上搜了下的demo,发现实现的效果并不是很理想,滑动偶尔会有延迟现象,所以自己想了个办法,顺便把自己实现写了个demo分享出来,一起来看看吧。

iOS中UIScrollView嵌套UITableView的实践教程

实现过程

最底部放置的为一个uiscrollview,设置scrollview的contentsize属性,使可以发生横向滚动,同时隐藏横向滚动条,设置代理为当前控制器本身。然后,在最底部的uiscrollview上放置2个uitableview,因为只有2个所以没有考虑重用问题,如果数量大于3个建议写下uiscrollview子视图的重用。最后在最上面覆盖一个topview,使得它可以和tableview发生纵向滚动,为了实现最上面的topview可以随着tableview发生一起滚动,需要在tableview的scrollviewdidscroll代理方法中获取tableview的contentoffset偏移量,随便改变topview的frame。

当手势点开始落在从topview上时候,在controller的loadview方法中设置自定义view,通过在自定义view中重载hittest方法,判断是否需要让tableview进行交互。此时需要注意的是因为有自定义的左右选择segmentcontrol,这么设置的时候segmentcontroller是不会相应点击方法的。为了让segmentcontroller可以实现随着tableview滚动并且可以相应单击事件,我在在controller的view上添加了单击手势,判定是否点击在了自定义的segmentcontroll上(因为tableview本身不会相应- (void)touchesbegan:(nsset<uitouch *> )touches withevent:(uievent )event事件,所以也可以自定义一个tablevuew,重载touchbegin 等方法,然后把tableview继承自这个tableview, 这样就可以相应相应的touchbegin等方法了), 好了,下面直接上代码

controller中代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma mark - 底部的scrollviuew的代理方法scrollviewdidscroll
 
- (void)scrollviewdidscroll:(uiscrollview *)scrollview
{
 cgfloat placeholderoffset = 0;
 if (self.topview.getselecteditemindex == 0) {
  if (self.firsttableview.contentoffset.y > self.topview.height - kitemheight) {
   placeholderoffset = self.topview.height - kitemheight;
  }
  else {
   placeholderoffset = self.firsttableview.contentoffset.y;
  }
  [self.secondtableview setcontentoffset:cgpointmake(0, placeholderoffset) animated:no];
 }
 else {
  if (self.secondtableview.contentoffset.y > self.topview.height - kitemheight) {
   placeholderoffset = self.topview.height - kitemheight;
  }
  else {
   placeholderoffset = self.secondtableview.contentoffset.y;
  }
  [self.firsttableview setcontentoffset:cgpointmake(0, placeholderoffset) animated:no];
 }
}
?
1
2
3
4
5
6
7
#pragma mark - 底部的scrollviuew的代理方法scrollviewdidenddecelerating
 
- (void)scrollviewdidenddecelerating:(uiscrollview *)scrollview
{
 nsinteger index = ceilf(scrollview.contentoffset.x / kscreen_width);
 self.topview.selecteditemindex = index;
}

controller中view的代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma mark - 重载系统的hittest方法
 
- (uiview *)hittest:(cgpoint)point withevent:(uievent *)event
{
 viewcontroller *currentvc = (viewcontroller *)self.nextresponder;
 currentvc.printpoint = point;
 if ([self.topview pointinside:point withevent:event]) {
  self.scrollview.scrollenabled = no;
  if (self.scrollview.contentoffset.x < kscreen_width *0.5) {
   return self.firsttableview;
  } else {
   return self.secondtableview;
  }
 } else {
  self.scrollview.scrollenabled = yes;
  return [super hittest:point withevent:event];
 }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma mark - 添加手势的相应方法
 
- (void)tapgestureaction:(uitapgesturerecognizer *)gesture
{
 cgpoint point = [gesture locationinview:self.topview];
 if (cgrectcontainspoint(self.topview.leftbtnframe, point)) {
  if (self.scrollview.contentoffset.x > 0.5 * kscreen_width) {
   [self.scrollview setcontentoffset:cgpointmake(0, 0) animated:no];
   self.topview.selecteditemindex = 0;
  }
 } else if (cgrectcontainspoint(self.topview.rightbtnframe, point)) {
  if (self.scrollview.contentoffset.x < 0.5 * kscreen_width) {
   [self.scrollview setcontentoffset:cgpointmake(kscreen_width, 0) animated:no];
   self.topview.selecteditemindex = 1;
  }
 }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma mark - firsttableview的代理方法scrollviewdidscroll
 
- (void)scrollviewdidscroll:(uiscrollview *)scrollview
{
 cgfloat placeholderheight = self.topview.height - self.topview.itemheight;
 
 cgfloat offsety = scrollview.contentoffset.y;
 
 if (offsety >= 0 && offsety <= placeholderheight) {
  self.topview.y = -offsety;
 }
 else if (offsety > placeholderheight) {
  self.topview.y = - placeholderheight;
 }
 else if (offsety <0) {
  self.topview.y = - offsety;
 }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma mark - secondtableview的代理方法scrollviewdidscroll
 
- (void)scrollviewdidscroll:(uiscrollview *)scrollview
{
 cgfloat placeholderheight = self.topview.height - self.topview.itemheight;
 cgfloat offsety = scrollview.contentoffset.y;
 if (offsety >= 0 && offsety <= placeholderheight) {
  self.topview.y = -offsety;
 } else if (offsety > placeholderheight) {
  self.topview.y = - placeholderheight;
 } else if (offsety <0) {
  self.topview.y = - offsety;
 }
}

完整项目下载地址如下:https://github.com/maxzhang123/nestscrollview

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://www.jianshu.com/p/c51665f7f48b

延伸 · 阅读

精彩推荐
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01