UIScrollViewDelegate scrollViewDidScroll 方法跟不上 high-velocity 滚动(轻弹)?
UIScrollViewDelegate scrollViewDidScroll method not keeping up with high-velocity scrolling (flick)?
我有一个 UIScrollView
和一个 UIView
header 粘在滚动视图的顶部,并在用户向下滚动时从全尺寸缩小到小尺寸。这些都是通过scroll view delegate实现的:
// scroll view delegate
extension SomeViewController {
// scroll view did scroll
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
let contentOffsetY = scrollView.contentOffset.y
// make banner sticky
banner.frame.origin.y = max(0, contentOffsetY)
// shrink banner
if contentOffsetY > 0 && contentOffsetY <= bannerHeight! - 64 {
banner.frame.size.height = bannerHeight! - contentOffsetY
bannerGraphic.frame = banner.bounds
}
}
}
效果很好...如果用户不高速滚动。如果用户轻弹向下滚动(高速),代表似乎没有跟上,横幅永远不会完全缩小(可能缩小 85-90%)。为了验证这一点,我将滚动视图的偏移量打印到控制台,并注意到当用户缓慢滚动时,控制台可能会打印 100 行当前偏移量。当用户快速滚动时,控制台可能会打印 25 行当前偏移量。代表根本无法跟上 high-velocity 滚动。
有没有办法确保无论滚动速度如何都可以缩小横幅,还是 UIKit
就是这样?
ScrollView Delegate 不会 收到视图滚动的每个点 的通知。而且您真的不希望那样...这将是太多不必要的处理。
因此,快速滚动时完全有可能出现较大的间隙。事实上,在快速测试中,我刚刚将 .contentOffsetY
值从 0 跳到 75 再到 300...
您的代码已关闭。您要做的是在您 超过 您的最大值时处理这种情况。
试一试(我认为它会插入并替换您的函数):
extension TestViewController: UIScrollViewDelegate {
// scroll view did scroll
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
let contentOffsetY = scrollView.contentOffset.y
// make banner sticky
banner.frame.origin.y = max(0, contentOffsetY)
// if scrollView content is all the way at the top
// (or pulled down, waiting to "bounce back up")
if contentOffsetY <= 0 {
// set banner to original height
banner.frame.size.height = bannerHeight
bannerGraphic.frame = banner.bounds
} else {
// view has scrolled (user has dragged *up*)
// set banner height to originalHeight - y offset,
// but keep it a minimum of 64
banner.frame.size.height = max(bannerHeight! - contentOffsetY, 64)
bannerGraphic.frame = banner.bounds
}
}
}
我有一个 UIScrollView
和一个 UIView
header 粘在滚动视图的顶部,并在用户向下滚动时从全尺寸缩小到小尺寸。这些都是通过scroll view delegate实现的:
// scroll view delegate
extension SomeViewController {
// scroll view did scroll
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
let contentOffsetY = scrollView.contentOffset.y
// make banner sticky
banner.frame.origin.y = max(0, contentOffsetY)
// shrink banner
if contentOffsetY > 0 && contentOffsetY <= bannerHeight! - 64 {
banner.frame.size.height = bannerHeight! - contentOffsetY
bannerGraphic.frame = banner.bounds
}
}
}
效果很好...如果用户不高速滚动。如果用户轻弹向下滚动(高速),代表似乎没有跟上,横幅永远不会完全缩小(可能缩小 85-90%)。为了验证这一点,我将滚动视图的偏移量打印到控制台,并注意到当用户缓慢滚动时,控制台可能会打印 100 行当前偏移量。当用户快速滚动时,控制台可能会打印 25 行当前偏移量。代表根本无法跟上 high-velocity 滚动。
有没有办法确保无论滚动速度如何都可以缩小横幅,还是 UIKit
就是这样?
ScrollView Delegate 不会 收到视图滚动的每个点 的通知。而且您真的不希望那样...这将是太多不必要的处理。
因此,快速滚动时完全有可能出现较大的间隙。事实上,在快速测试中,我刚刚将 .contentOffsetY
值从 0 跳到 75 再到 300...
您的代码已关闭。您要做的是在您 超过 您的最大值时处理这种情况。
试一试(我认为它会插入并替换您的函数):
extension TestViewController: UIScrollViewDelegate {
// scroll view did scroll
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
let contentOffsetY = scrollView.contentOffset.y
// make banner sticky
banner.frame.origin.y = max(0, contentOffsetY)
// if scrollView content is all the way at the top
// (or pulled down, waiting to "bounce back up")
if contentOffsetY <= 0 {
// set banner to original height
banner.frame.size.height = bannerHeight
bannerGraphic.frame = banner.bounds
} else {
// view has scrolled (user has dragged *up*)
// set banner height to originalHeight - y offset,
// but keep it a minimum of 64
banner.frame.size.height = max(bannerHeight! - contentOffsetY, 64)
bannerGraphic.frame = banner.bounds
}
}
}