Ios 检测UiScrollView上的触摸位置
我需要从UIScrollView获取触摸位置。但当我为scrollview创建子类时:Ios 检测UiScrollView上的触摸位置,ios,animation,uiscrollview,touch,Ios,Animation,Uiscrollview,Touch,我需要从UIScrollView获取触摸位置。但当我为scrollview创建子类时: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSArray *touchesArray = [touches allObjects]; UITouch *touch = (UITouch *)[touchesArray objectAtIndex:0]; CGPoint point = [touch locat
- (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;}
并非总是调用函数touchsbegind
。
如果我快速滑动,touchsbegind
永远不会被调用,但是scrollviewdidcroll
会被直接调用
我不能使用ui手势(UITapGestureRecognizer,…)
,因为用户不点击或滑动
是否有其他方法可以从UIScrollView获得该职位
编辑:
谢谢ThXou:
UIScrollView子类:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
self.position = point;
return self;
}
要检测scrollview内的触摸位置,请尝试此代码
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:scrollView];
}
我看到了,所以我认为它可以解决你的问题。SDK似乎不再将触摸处理方法传递给UIScrollView子类。因此,您应该执行一个hitTest:
来将触摸传递给您的子类
有关更多信息,请参见答案。您也可以使用
scrollview.pangestureRecognitizer
的locationInView
方法获取视图中触摸的坐标。根据您的需求,可以在任何scrollView
的委托方法中使用
scrollView.delaysContentTouches = NO;
它将使触摸开始,并在scrollViewDidScroll之前触发其他触摸事件
hitTest是一种黑客攻击,不应成为首选解决方案。但在滚动我的滚动视图时,从未调用“touchesBegind”或“touchesMoved”功能。我想在使用scrollview时获得触摸。即使“touchesBegind”为空,也不会调用她。抱歉,请尝试上面的代码。另一种方法是需要调用子类。有关更多详细信息,请查看此链接以检测ToucheSBegind和touchesMoved,不需要进行子分类。您可以将UIScrollViewDelegate添加到“h”文件和适用的函数,以根据用户意图检测触摸。见Thx!我的代码:'-(UIView*)hitTest:(CGPoint)pointwithevent:(UIEvent*)event{self.position=point;return self;}'