我有一个包含图像和表格视图的视图。图像在屏幕的前半部分(纵向),表视图在后半部分。现在我想在图像上移动tableview,直到它到达顶部并覆盖图像。然后它应该开始“真正的滚动”。
但是最好的方法是什么呢?我可以替换变量tableView的touchesMove吗?我可以创建UITableView的扩展并覆盖该函数,但这样我就无法访问控制器的视图来移动tableView。
有答案吗?谢谢!
发布于 2017-01-05 07:10:29
imageView应该在tableView后面,约束为顶部、前导、尾部到超视图,高度到超视图,乘数为0.5。tableView应该填充它的superview。
诀窍是添加一个不可见的tableViewHeader,它等于屏幕高度的一半。这具有将tableView的初始内容从屏幕上推送出去的效果。在接口构建器中,将UIView添加到tableView as标头,并使其透明。也要使你的tableView的背景透明。连接headerView和tableView的电源插座。在viewDidLayoutSubviews中,设置您的headerView.frame.size.height = tableView.frame.size.height / 2。
发布于 2017-01-05 13:41:58
除了Josh的回答之外,你可以尝试的是:
你可以做的是让UIImageView的高度根据滚动量减少,直到UIImageView的高度为0,然后开始滚动,否则强制UITableView的contentOffSet始终保持0。也就是说,这里是如何做到的:
创建一个枚举来跟踪UIImageView的各种状态,如下所示:
enum Layout {
case ImageExpanding,
case ImageDefaultHeight,
case ImageDiminishing,
case ImageNotVisible
}在你的故事板中,添加一个顶部,前导和尾随约束到你的UIImageView的superview和固定高度约束,比如200(不用担心你以后会改变这个)。为你的UITableView添加一个前导、尾部和底部约束到superview,并在UIImageView上添加一个顶部约束。现在,将UIImageView高度的约束出口拖放到UIViewController中。
在viewDidLoad中,将heightConstraint设置为总屏幕高度的1/2,并将枚举状态初始设置为ImageDefaultHeight
现在在你的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)
}
}https://stackoverflow.com/questions/41474533
复制相似问题