我有这样一段代码,当对象被平移时,它会检索对象的坐标:
UITapGestureRecognizer *moveBuildingTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(moveobject:)];方法moveobject内容:
CGPoint tapPoint=[recognizer locationOfTouch:0 inView:self.view];我使用这些坐标来改变- move it -an imageview的框架。
但是,在拖动图像时-触发索引操作,我发现当我将它拖到绝对底部时,我得到了一个错误,-UIPanGestureRecognizer locationOfTouch:inView::index (0)超出了界限(0)。
如何解决此异常并防止用户拖过此点?
谢谢
发布于 2012-11-06 06:43:40
奇怪的是,即使手势识别器的私有touches数组似乎是空的,也会调用您的moveobject:方法。
总之,一般来说,如果你不能在手势识别器中处理多点触控手势,我建议使用[recognizer locationInView:]而不是locationOfTouch:inView:。
顺便说一句:
当您在代码中使用UITapGestureRecognizer时,您谈论的是UIPanGestureRecognizer。
我推荐的处理拖动特定视图的代码如下所示:
//...
UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[someView addGestureRecognizer:panGR];
//...
- (void)handlePan:(UIPanGestureRecognizer *)gr
{
CGPoint translation = [gr translationInView:gr.view];
gr.view.frame = CGRectOffset(gr.view.frame, translation.x, translation.y);
[gr setTranslation:CGPointZero inView:gr.view];
}发布于 2015-04-24 20:18:16
您应该检查UIGestureRecognizer中的numberOfTouches,如下所示:
if (recognizer.numberOfTouches) {
CGPoint tapPoint = [recognizer locationOfTouch:0 inView:self.view];
}https://stackoverflow.com/questions/13239478
复制相似问题