[英]Find X position of where user is dragging their finger in ScrollView
I have a scrollview and I'm watching user input. 我有一个滚动视图,并且正在监视用户输入。 I would like to know where their finger currently is on the X plane.
我想知道他们的手指当前在X平面上的位置。 Is this possible through the ScrollView and/or its delegate or do I have to override
touchesBegan
, etc? 是否可以通过ScrollView和/或其委托进行操作,还是必须重写
touchesBegan
等?
I'm assuming you set up your scroll view delegate. 我假设您设置了滚动视图委托。 If you did, then you only need to implement the
scrollViewDidScroll:
method from the UIScrollViewDelegate
protocol... 如果这样做了,那么您只需要从
UIScrollViewDelegate
协议中实现scrollViewDidScroll:
方法...
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint touchPoint = [scrollView.panGestureRecognizer locationInView:scrollView];
NSLog(@"Touch point: (%f, %f)", touchPoint.x, touchPoint.y);
}
This will update while the user is scrolling. 这将在用户滚动时更新。
Additional info: Note that if you have something like a UIPageControl
, that your scroll view is navigating between, you may have to calculate the x position based on the number of pages (ie if each page is 100 pixels wide, page 0 will start at point 0, page one at point 100, etc..). 附加信息:请注意,如果您具有
UIPageControl
类的UIPageControl
,并且您正在滚动视图之间进行导航,则可能必须根据页面数来计算x位置(即,如果每个页面的宽度为100像素,则页面0将从以下位置开始)点0,第一页指向点100,依此类推)。
CGPoint touchPoint = [scrollView.panGestureRecognizer locationInView:scrollView];
CGFloat x = touchPoint.x;
CGPoint point = [_scrollView locationOfTouch:touchIndex inView:_scrollView];
touchindex: 0 for first touch and 1, 2,3 so on touchindex:第一次触摸为0,1,2,3,依此类推
if inview:nil then point will be in the window base coordinate sysytem 如果inview:nil,则点将在窗口基本坐标系中
CGFloat x = point.x;
CGFloat y = point.y
I use the panGesture of the scrollView because it's more accurate if you want to know the position of the x plane when you have only vertical scroll. 我使用scrollView的panGesture,因为当您只有垂直滚动时,如果您想知道x平面的位置,它会更准确。
[self.scrollView.panGestureRecognizer addTarget:self action:@selector(handlePanForScrollView:)];
and then: 接着:
- (void)handlePanForScrollView:(UIPanGestureRecognizer *)gesture {
CGPoint positionInView = [gesture locationInView:self.scrollView];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.