即使内容大小适合设备屏幕大小,我也想像UITableView一样使滚动视图上下滚动。但是,向上滚动后,最终内容大小必须与起始内容大小相同(为零)。有办法吗?
[当用户向下滚动时像UITableView拉伸效果一样,这里有一个代码捕捉跳动效果
class ScrollViewController: UIScrollViewDelegate {
private var topViewHeightConstraintConstant: CGFloat = 259
@IBOutlet weak var TopViewHeightConstraint: NSLayoutConstraint!
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView === self.scrollView {
if scrollView.isBouncingBottom || scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height {
scrollView.setContentOffset(CGPoint(x: scrollView.contentOffset.x, y: scrollView.contentSize.height - scrollView.frame.size.height), animated: false)
}
else if scrollView.isBouncingTop && self.TopViewHeightConstraint.constant == topViewHeightConstraintConstant {
scrollView.setContentOffset(CGPoint(x: scrollView.contentOffset.x, y: 0), animated: false)
}
if scrollView.contentOffset.y < 0 {
if self.TopViewHeightConstraint.constant + abs(scrollView.contentOffset.y) <= topViewHeightConstraintConstant {
self.TopViewHeightConstraint.constant += abs(scrollView.contentOffset.y)
}
} else if scrollView.contentOffset.y > 0 && self.TopViewHeightConstraint.constant >=
(self.view.frame.height - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom - self.contentViewHeight.constant) {
self.TopViewHeightConstraint.constant -= scrollView.contentOffset.y
scrollView.setContentOffset(CGPoint(x: scrollView.contentOffset.x, y: 0), animated: false)
}
view.layoutIfNeeded()
}
}
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if scrollView !== self.scrollView {
TopViewHeightConstraint.constant = topViewHeightConstraintConstant
}
}
}