- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望我的垂直 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/
documentation声明它是 points(每秒,我假设)?但是,我得到的值在 (.5, 3.5) 范围内。然后 ScrollView 在停止之前移动了数百个点。实际初速度应该相差几个数量级。
我在一个 View Controller 上有两个 UITableViews( View Controller 是他们的代表)。其中之一将取决于 scrollViewWillEndDragging:w
我正在尝试实现一个自定义标签栏,它是可滚动的并且在每个标签栏项目上都有分页。为此,我正在使用代理 scrollViewWillEndDragging:withVelocity:targetConten
如何用 Swift 编写此代码? 更新:发布了我试图转换的方法,最初发布了一个不同的方法,这不是我试图隐藏的方法 这适用于 Objective C,但不适用于 Swift。当我尝试将 ceilf 和
我希望我的垂直 UIScrollView 仅在用户向上滚动时停在最大内容偏移处,或者在用户向下滚动时停在最小内容偏移处。 我正在使用以下两个函数 func targetContentOffset(fo
我是一名优秀的程序员,十分优秀!