UiScrollview scrollviewDidScroll 检测用户触摸
Posted
技术标签:
【中文标题】UiScrollview scrollviewDidScroll 检测用户触摸【英文标题】:UiScrollview scrollviewDidScroll detect user touch 【发布时间】:2014-07-05 09:31:50 【问题描述】:我在我的应用程序中使用UIScrollView
,并使用scrollviewDidScroll
方法来移动不在滚动视图内的其他一些视图。这种行为应该只在用户实际用手指移动滚动视图时发生,而不是在触摸事件结束时发生。
目前我正在使用滚动视图的dragging
属性来获取此行为,但它有一个问题:当用户在滚动视图上滑动并在停止减速之前再次触摸它时,dragging
属性不正确尽管用户正在用手指拖动滚动视图。
【问题讨论】:
查看您的 tableView 的UIGestureRecognizerState
,例如 yourTableView.panGestureRecognizer.state
。它将揭示用户触摸的确切状态。在scrollViewDidScroll
中进行操作。
完成。附示例代码
【参考方案1】:
UIScrollView
有一个panGestureRecogniser
属性,用于跟踪滚动视图中的平移手势。您可以在scrollViewDidScroll
中跟踪平移手势的state
,以准确了解平移处于什么状态。
Swift 4.0
func scrollViewDidScroll(_ scrollView: UIScrollView)
if scrollView.isEqual(yourScrollView)
switch scrollView.panGestureRecognizer.state
case .began:
// User began dragging
print("began")
case .changed:
// User is currently dragging the scroll view
print("changed")
case .possible:
// The scroll view scrolling but the user is no longer touching the scrollview (table is decelerating)
print("possible")
default:
break
Objective-C
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
if ([scrollView isEqual:yourScrollView])
switch (scrollView.panGestureRecognizer.state)
case UIGestureRecognizerStateBegan:
// User began dragging
break;
case UIGestureRecognizerStateChanged:
// User is currently dragging the scroll view
break;
case UIGestureRecognizerStatePossible:
// The scroll view scrolling but the user is no longer touching the scrollview (table is decelerating)
break;
default:
break;
【讨论】:
【参考方案2】:IMO 更好的答案是使用UIScrollViewDelegate
,有以下通知:
// called on start of dragging (may require some time and or distance to move)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
// called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_ios(5_0);
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
【讨论】:
您还需要收听scrollViewDidScrollToTop
。
@kelin 这与用户触摸有什么关系?
当用户双击状态栏时调用此事件。这导致表格视图滚动到顶部。在UIScrollViewDelegate
文档中阅读更多信息。
这与问题有什么关系?【参考方案3】:
var lastVelocityYSign = 0
func scrollViewDidScroll(_ scrollView: UIScrollView)
let currentVelocityY = scrollView.panGestureRecognizer.velocity(in: scrollView.superview).y
let currentVelocityYSign = Int(currentVelocityY).signum()
if currentVelocityYSign != lastVelocityYSign &&
currentVelocityYSign != 0
lastVelocityYSign = currentVelocityYSign
if lastVelocityYSign < 0
print("SCROLLING DOWN")
else if lastVelocityYSign > 0
print("SCOLLING UP")
Mos6y 的回答 https://medium.com/@Mos6yCanSwift/swift-ios-determine-scroll-direction-d48a2327a004
【讨论】:
以上是关于UiScrollview scrollviewDidScroll 检测用户触摸的主要内容,如果未能解决你的问题,请参考以下文章
UIScrollView 内的 UIScrollView 时滚动
UIScrollView里面的UIScrollView:滚动
问题:UIScrollview 弹跳使父 UIScrollview 弹跳
UIScrollView 嵌入其他 UIScrollView