gpt4 book ai didi

ios - 如何使用 scrollViewWillEndDragging :withVelocity:targetContentOffset to ensure only stops scrolling at two possible positions?

转载 作者:行者123 更新时间:2023-11-28 07:32:56 25 4
gpt4 key购买 nike

我希望我的垂直 UIScrollView 仅在用户向上滚动时停在最大内容偏移处,或者在用户向下滚动时停在最小内容偏移处。

我正在使用以下两个函数

func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint,
withScrollingVelocity velocity: CGPoint) -> CGPoint {
return CGPoint(x:0,y:self.scrollView.contentSize.height - self.scrollView.bounds.height)
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

if velocity.y > 0 {
targetContentOffset.pointee.y = scrollView.contentSize.height - scrollView.bounds.height
} else {
targetContentOffset.pointee.y = CGFloat(0)
}
}

第一个函数永远不会被调用。当我向上或向下滚动时,第二个被正确调用,但设置 pointee.y 值似乎并没有改变 contentOffset - 例如,我仍然可以滚动并停在中间。我怎样才能做到这一点?

最佳答案

也许在 ScrollView 上添加 View 并添加滑动手势,如下所示:

 var swipeGesture  = UISwipeGestureRecognizer()
let directions: [UISwipeGestureRecognizer.Direction] = [.up, .down]
for direction in directions {
swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(swipeView(_:)))
view.addGestureRecognizer(swipeGesture)
swipeGesture.direction = direction
view.isUserInteractionEnabled = true
view.isMultipleTouchEnabled = true
}


self.view = view
scrollView.delegate = self


@objc func swipeView(_ sender : UISwipeGestureRecognizer){
if sender.direction == .up {
scrollView.setContentOffset(CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.height), animated: true)
} else if sender.direction == .down {
scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
}
}

关于ios - 如何使用 scrollViewWillEndDragging :withVelocity:targetContentOffset to ensure only stops scrolling at two possible positions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54151919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com