PAN Gesture is crashing while taking the location of touch in view in iOS

对着背影说爱祢 提交于 2019-12-23 20:06:30

问题


I just do a sample to check the pan gesture.

The pan gesture is detecting and working fine.

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

The console is giving the message

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

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

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.

I tried in this way.

- (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 ) 
    {
    }

}

回答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.




回答2:


I too came across this error despite the max and min number of touches being set. I'm subclassing my gesture recognizer and figure it has something to do with that. I got around it by simply checking numberOfTouches before referencing it:

if ([gestureRecognizer numberOfTouches] > 0) {
    CGPoint point = [gestureRecognizer locationOfTouch:0 inView:self.superview.window];
}

Hope this helps someone!




回答3:


The error is telling you that on this line:

    CGPoint secondPoint = [sender locationOfTouch:1 inView:self.imageView];

index "1" is out of locationOfTouches bounds. So, as stated above, you need to make sure you set minimumNumberOfTouches

Additionally, you will want to enabled user interaction on the image view in order for it to respond to gesture recognizers.

[self.imageView setUserInteractionEnabled:YES];



回答4:


When the max/min touches are set, they determines whether it is a valid gesture to begin sending action but they are not the criteria for ending it. For example, if you set the max/min to 2. If a two-finger-touch is detected, the handler starts receiving the action. Leaving one finger will not end the gesture. The handler still receive action with change-state and one touch. In the end, the handler receives 0 touch and end-state.



来源:https://stackoverflow.com/questions/12066821/pan-gesture-is-crashing-while-taking-the-location-of-touch-in-view-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!