检测UiScrollView上的触摸位置
我需要从UIScrollView获取触摸位置。 但是当我为我的scrollview创build一个子类:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSArray *touchesArray = [touches allObjects]; UITouch *touch = (UITouch *)[touchesArray objectAtIndex:0]; CGPoint point = [touch locationInView:self]; self.position = point;}
touchesBegan
函数并不总是被调用。 如果我快速滑动, touchesBegan
永远不会被调用,但是直接调用scrollViewDidScroll
。
我无法使用UIGesture (UITapGestureRecognizer, ... )
因为用户不点击或滑动。
有没有其他的方法从UIScrollView获取位置?
编辑:
感谢ThXou: https ://stackoverflow.com/a/15763450/1752843
UIScrollView子类:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ self.position = point; return self; }
我看到了一个答案 ,我认为它可以解决你的问题。 看来,SDK不再将触摸处理方法传递给UIScrollView子类。 所以,你应该执行hitTest:
将触摸传递给你的子类。
请参阅答案以获取更多信息。
要检测滚动视图中的触摸位置,请尝试使用此代码
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; [scrollView addGestureRecognizer:singleTap]; - (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture { CGPoint touchPoint=[gesture locationInView:scrollView]; }
您也可以使用scrollview.panGestureRecognizer
的locationInView
方法来获取视图中的触摸坐标。 这可以在任何scrollView
的委托方法取决于您的要求。
scrollView.delaysContentTouches = NO;
这将使得touchesBegan和其他触摸事件将在scrollViewDidScroll之前被触发。
hitTest是一个黑客,不应该是首选的解决scheme。
- 如何在iOS 7 iPad App Store中同时翻转和放大UIView?
- 隐式animation究竟在iOS中发生?
- 在reloadItemsAtIndexPaths之后避免UICollectionView的animation
- 脚本animation阻止iOS
- 在UIImageView中向上和向下移动UIImage(像hover)循环
- animation一个圆圈成长
- CALayer – CABasicAnimation无法缩放中心/ anchorPoint
- iOS设备旋转即时捕捉而不是animation
- iOS的GoogleMaps SDK – animateToCameraPositionanimation完成处理程序?