在iOS中查看触摸位置时PAN手势崩溃 [英] PAN Gesture is crashing while taking the location of touch in view in iOS

查看:72
本文介绍了在iOS中查看触摸位置时PAN手势崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是做一个样本来检查平移手势.

I just do a sample to check the pan gesture.

平移手势可以检测并正常工作.

The pan gesture is detecting and working fine.

但是每当我像 CGPoint secondPoint = [sender locationOfTouch:1 inView:self.imageView]; 这样的平移手势中给secondPoint时,它就会崩溃.

But whenever i give a secondPoint in the pan gesture like CGPoint secondPoint = [sender locationOfTouch:1 inView:self.imageView]; it is crashing.

控制台正在显示消息

 *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UIPanGestureRecognizer locationOfTouch:inView:]: index (1) beyond bounds (1).'

当我使用 panGestureRecognizer.maximumNumberOfTouches = 1; panGestureRecognizer.minimumNumberOfTouches = 1;仍然崩溃.

When I use panGestureRecognizer.maximumNumberOfTouches = 1; panGestureRecognizer.minimumNumberOfTouches =1; still it is crashing.

当我使用 panGestureRecognizer.maximumNumberOfTouches = 2; panGestureRecognizer.minimumNumberOfTouches = 2; 那么它就没有进入

When I use panGestureRecognizer.maximumNumberOfTouches = 2; panGestureRecognizer.minimumNumberOfTouches = 2; then it is not entering into the

- (void)panGestureHandler:(UIPanGestureRecognizer *)sender method.

任何人都可以指导我出现问题的地方.

Can anyone please guide me where im going wrong.

预先感谢.希望能为您提供帮助.

Thanks in advance.Hoping for your help.

我以这种方式尝试过.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureHandler:)];
     panGestureRecognizer.maximumNumberOfTouches = 2;
     [self.imageView addGestureRecognizer:panGestureRecognizer];

}
- (void)panGestureHandler:(UIPanGestureRecognizer *)sender
{
    if ([sender state] == UIGestureRecognizerStateBegan )
    {
        CGPoint firstPoint = [sender locationOfTouch:0 inView:self.imageView];
        CGPoint secondPoint = [sender locationOfTouch:1 inView:self.imageView];
    }
    else if ([sender state] ==UIGestureRecognizerStateEnded ) 
    {
    }

}

推荐答案

您提供了 maximumNumberOfTouches ,但没有提供 minimumNumberOfTouches .即,可以在第一次触摸后识别手势.在这种情况下,可能没有第二次接触,并且索引 1 (引用第二个元素)超出了数组范围.

You provided a maximumNumberOfTouches, but no minimumNumberOfTouches. I.e., the gesture can be recognized after the first touch. In this case, no second touch may exist, and your index 1 (referring the second element) exceeds the array bounds.

这篇关于在iOS中查看触摸位置时PAN手势崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆