我在
UITableView
其中,当用户拉下桌子时,图像被拉伸,当
UITableView
拉起或放开时,图像将缩小回其原始大小。
下面是一个演示:
我有一个
UIView
包含
UIImageView
将其内容模式设置为
方面填充
.
下面是一个
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
正在下拉以展开图像,一次
ImageContainerViewWightConstraint。常量=>initialContainerImageViewHeight*2
在内部
滚动视图DidScroll
,我想防止
UITableView
不会再被拉下。
目前看起来是这样的:
有没有办法防止
UITableView
当满足上述条件时,不会被进一步拉下,但是
还
允许
UITableView
待拉
支持
?
根据建议
Sh\u 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)
}
结果是: