我已经在UITableView
之上实现了一个拉伸标题,当用户拉下桌子时,图像会拉伸,当UITableView
被拉起或放开时,图像减少到原来的大小。
这是一个演示:
我有一个UIView
容器,其中包含UIImageView
,其内容模式设置为 Aspect Fill 。
下面是UITableView
,可以在演示中看到。
我使用UITableView's
scrollView委托方法来确定何时拉伸和缩小容器视图,如下所示:
extension MyViewController: UIScrollViewDelegate
{
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
if scrollView.contentOffset.y < 0 &&
imageContainerViewHeightConstraint.constant < initialContainerImageViewHeight * 2
{
imageContainerViewHeightConstraint.constant += abs(scrollView.contentOffset.y)
}
else if scrollView.contentOffset.y > 0 &&
imageContainerViewHeightConstraint.constant > initialContainerImageViewHeight
{
imageContainerViewHeightConstraint.constant -= abs(scrollView.contentOffset.y)
}
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
resetContainerViewSize()
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
{
resetContainerViewSize()
}
}
func resetContainerViewSize()
{
imageContainerViewHeightConstraint.constant = initialContainerImageViewHeight
UIView.animate(withDuration: 0.4,
delay: 0.0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.5,
options: .curveEaseInOut,
animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
如果scrollView.contentOffset.y < 0
中的imageContainerViewHeightConstraint.constant => initialContainerImageViewHeight * 2
和scrollViewDidScroll
,则容器视图会停止拉伸图像。
我想要实现的是,当UITableView
被拉下来展开图片时,imageContainerViewHeightConstraint.constant => initialContainerImageViewHeight * 2
内的scrollViewDidScroll
,我想阻止UITableView
不再被拉下来。
目前它看起来像这样:
有没有办法阻止UITableView
在满足上述条件时被进一步拉下来,但是仍然允许UITableView
被拉回向上
根据 Sh_Khan:
的建议extension MyViewController: UIScrollViewDelegate
{
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
if scrollView.contentOffset.y < 0 &&
imageContainerViewHeightConstraint.constant < initialContainerImageViewHeight * 2
{
imageContainerViewHeightConstraint.constant += abs(scrollView.contentOffset.y)
}
else if scrollView.contentOffset.y < 0 && imageContainerViewHeightConstraint.constant >= initialContainerImageViewHeight * 2
{
imageContainerViewHeightConstraint.constant = initialContainerImageViewHeight * 2
view.layoutIfNeeded()
dataTableView.frame = CGRect.init(x: 0.0,
y: initialContainerImageViewHeight * 2,
width: dataTableView.frame.size.width,
height: dataTableView.frame.size.height)
return
}
else if scrollView.contentOffset.y > 0 &&
imageContainerViewHeightConstraint.constant > initialContainerImageViewHeight
{
imageContainerViewHeightConstraint.constant -= abs(scrollView.contentOffset.y)
}
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
resetContainerViewSize()
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
{
resetContainerViewSize()
}
}
func resetContainerViewSize()
{
imageContainerViewHeightConstraint.constant = initialContainerImageViewHeight
UIView.animate(withDuration: 0.4,
delay: 0.0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.5,
options: .curveEaseInOut,
animations: {
self.view.layoutIfNeeded()
self.dataTableView.frame = CGRect.init(x: 0.0,
y: self.initialContainerImageViewHeight,
width: self.dataTableView.frame.size.width,
height: self.dataTableView.frame.size.height)
}, completion: nil)
}
结果:
答案 0 :(得分:1)
试试这个
extension ViewController: UIScrollViewDelegate
{
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
print("ddffddfd \(scrollView.contentOffset.y)")
if scrollView.contentOffset.y > 0 && imageContainerViewHeightConstraint.constant == 223
{
return
}
if scrollView.contentOffset.y > 0
{
var sd = imageContainerViewHeightConstraint.constant + abs(scrollView.contentOffset.y)
if(sd < 233 )
{
print("path111 1")
self.dataTableView.contentOffset = CGPoint.init(x: 0, y: 0 )
return
}
else
{
print("path111 2")
imageContainerViewHeightConstraint.constant -= abs(scrollView.contentOffset.y)
}
print("path11111 3")
view.layoutIfNeeded()
self.dataTableView.contentOffset = CGPoint.init(x: 0, y: 0 )
return
}
print("path11111 4")
if scrollView.contentOffset.y < 0 && imageContainerViewHeightConstraint.constant >= initialContainerImageViewHeight * 2
{
self.dataTableView.contentOffset = CGPoint.init(x: 0, y: 0 )
//self.dataTableView.bounces = false
return
}
else
{
imageContainerViewHeightConstraint.constant += abs(scrollView.contentOffset.y)
view.layoutIfNeeded()
}
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
resetContainerViewSize()
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
{
resetContainerViewSize()
}
}
///////
func resetContainerViewSize()
{
imageContainerViewHeightConstraint.constant = 223
UIView.animate(withDuration: 0.7,
delay: 0.0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.5,
options: .curveEaseInOut,
animations: {
self.view.layoutIfNeeded()
self.dataTableView.bounces = true
}, completion: nil)
}
在行动中
在此处查找演示testScrollAboveTable