【问题标题】:iOS - Move TableView before scrollingiOS - 在滚动前移动 TableView
【发布时间】:2017-01-04 22:48:08
【问题描述】:

我有一个带有图像和表格视图的视图。图像位于屏幕的前半部分(纵向),而表视图位于后半部分。现在我想将表格视图移动到图像上,直到它到达顶部并覆盖图像。然后它应该开始“真正的滚动”。

但是最好的方法是什么?我可以替换变量tableView的touchesMove吗?我可以创建 UITableView 的扩展并覆盖该函数,但是我无法访问我的控制器的视图来移动 tableView。

有答案吗?谢谢!

【问题讨论】:

    标签: ios swift uitableview cocoa-touch tableview


    【解决方案1】:

    imageView 应该在 tableView 后面,约束 top、leading、 trailing 到 superview 和 height 到 superview,乘数为 0.5。 tableView 应该填充它的父视图。

    诀窍是你添加一个不可见的tableViewHeader,它等于屏幕高度的一半。这具有将 tableView 的初始内容推离屏幕的效果。在界面生成器中,将 UIView 作为标题添加到 tableView 并使其透明。还要使 tableView 的背景透明。为 headerView 和 tableView 提供一个出口。在 viewDidLayoutSubviews 中设置你的 headerView.frame.size.height = tableView.frame.size.height / 2.

    【讨论】:

    • 谢谢!但是我怎样才能让标题透明呢?我创建了一个新视图,将 alpha 设置为 0.0,将背景设置为 UIColor.clear。界限是 x:0, y:0, width: screen width, height: screen height /2 。然后我用 tableView.tableHeaderView = headerView 设置它。我说的对吗?
    【解决方案2】:

    除了什么乔希回答,你可以尝试是: P>

    你可以做的是使UIImageView的高度降低基于滚动量,直到UIImageView的高度为0,然后开始否则滚动力contentOffSet的的UITableView永远保持为0这样说,这里是如何做到这一点:

    请枚举保持UIImageView的各种状态的轨道,如:

    enum Layout {
    
         case ImageExpanding,
         case ImageDefaultHeight,
         case ImageDiminishing,
         case ImageNotVisible
    }
    

    在你的故事板,加顶,开头和结尾的约束SuperView把你UIImageView和固定高度约束可以说,200(别担心,你会改变这一点)。为了您的UITableView添加一个领导,尾随和底部约束到上海华和顶部的UIImageView。现在拖放一个约束出口为UIImageView高自己的UIViewController。 P>

    在您的viewDidLoad设定heightConstraint是总屏幕高度的1/2,枚举状态设置为初始地ImageDefaultHeight P>

    现在,在您的scrollViewDidScroll你将要检查的滚动的方向,并基于该在检查的形象,增加当前状态或减少heightConstraint基于量由用户滚动,并经常检查:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
         if(/*up*/){
              //just telling you about this if condition as this will ensure that 
              //if your table view is large and if tableview isn't at the top, 
              //the image shouldn't expand
    
               if layout == .ImageNotVisible{
    
                   if scrollView.contentOffset.y <= 0 {
    
                        //start expanding once you reach top of tableview
                        layout = .ImageExpanding;
                   }
               }
    
              //keep track of other enum states and update your uiimageview 
              //appropriately after calculating the user scroll amount         
              //until the height reaches your initialDefaultHeight and 
              //then do nothing. You will have to figure out the code 
              //for this on your own
    
    
         }else if(/*down*/){
    
              //make image smaller
         }
    
         //dont let table view scroll until image height is 0 when use is scrolling down
         if layout != ImageNotVisible{
    
             scrollView.contentOffset = CGPoint(x: 0, y: 0)
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-01
      • 2017-07-08
      • 2013-12-29
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多