可能是两个手指在屏幕上触摸调用手柄Pan方法



我的代码有问题,我需要处理屏幕上的平移或捏合。

但有时当我的两根手指在屏幕上时,手柄Panl 方法会调用。

我做错了什么?

-(void)addGestureRecognizer
{
// handle Pinch
imageViewForCrop.userInteractionEnabled = YES;
UIPinchGestureRecognizer *PinchOnScreen = [[UIPinchGestureRecognizer alloc]
                                           initWithTarget:self action:@selector(handlePinch:)];
[imageViewForCrop addGestureRecognizer:PinchOnScreen];
// handle Pan
UIPanGestureRecognizer *panOnScreen = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[imageViewForCrop addGestureRecognizer:panOnScreen];

}

这是平移方法:

 -(void)handlePan:(UIPanGestureRecognizer *)recognizer
{


switch (recognizer.state)
{
    case UIGestureRecognizerStateChanged:
    {
        NSLog(@"%lu",(unsigned long)recognizer.numberOfTouches);
        CGPoint translation = [recognizer translationInView:imageViewForCrop];

        if (image.size.width > image.size.height) // weight image
        {
            // after user pinch in the screen
            if (imageViewForCrop.frame.size.height > self.view.frame.size.height - bottomImageCropView.frame.size.height)
                                {

                                    // allow dragging only in Y coordinates by only updating the Y coordenates with translation position
                                    recognizer.view.center = CGPointMake(recognizer.view.center.x, recognizer.view.center.y + translation.y);

                                    [recognizer setTranslation:CGPointMake(0, 0) inView:imageViewForCrop];

                                    // get the top edge coordinate for the top left corner of crop frame
                                    float topEdgeImagePosition = CGRectGetMinY(imageViewForCrop.frame);
                                    float topEdgeCropEreaPosition = CGRectGetMinY(cropErea.frame);

                                    //get the buttom edge coordinate for bottom left corner of crop frame
                                    float bottomEdgeImagePosition = CGRectGetMaxY(imageViewForCrop.frame);
                                    float bottomEdgeCropEreaPosition = CGRectGetMaxY(cropErea.frame);
                                    //
                                    // if the top edge coordinate is less than or equal to Crop Erea frame
                                    if (topEdgeImagePosition > topEdgeCropEreaPosition)
                                    {
                                        // draw drag view in max top position
                                        imageViewForCrop.frame = CGRectMake(imageViewForCrop.frame.origin.x,topEdgeCropEreaPosition,imageViewForCrop.frame.size.width,imageViewForCrop.frame.size.height);
                                    }
                                    // if bottom edge coordinate is greater than or equal to 480
                                    if (bottomEdgeImagePosition < bottomEdgeCropEreaPosition)
                                    {
                                        float ratio = (imageViewForCrop.frame.size.height - cropErea.frame.size.height) * -1;
                                        // draw drag view in max bottom position
                                        imageViewForCrop.frame = CGRectMake(imageViewForCrop.frame.origin.x,ratio,imageViewForCrop.frame.size.width,imageViewForCrop.frame.size.height);
                                    }

                                }


            // allow dragging only in X coordinates by only updating the X coordenates with translation position
            recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y);
            [recognizer setTranslation:CGPointMake(0, 0) inView:imageViewForCrop];

            // get the top edge coordinate for the top left corner of crop frame
            float leftEdgeImagePosition = CGRectGetMinX(imageViewForCrop.frame);
            float leftEdgeCropEreaPosition = CGRectGetMinX(cropErea.frame);

            //get the buttom edge coordinate for bottom left corner of crop frame
            float rightEdgeImagePosition = CGRectGetMaxX(imageViewForCrop.frame);
            float rightEdgeCropEreaPosition = CGRectGetMaxX(cropErea.frame);
            //
            // if the top edge coordinate is less than or equal to Crop Erea frame
            if (leftEdgeImagePosition > leftEdgeCropEreaPosition)
            {
                // draw drag view in max top position
                imageViewForCrop.frame = CGRectMake(leftEdgeCropEreaPosition,0,imageViewForCrop.frame.size.width,imageViewForCrop.frame.size.height);
            }
            // if bottom edge coordinate is greater than or equal to 480
            if (rightEdgeImagePosition < rightEdgeCropEreaPosition)
            {
                float ratio = (imageViewForCrop.frame.size.width - cropErea.frame.size.width) * -1;
                // draw drag view in max bottom position
                imageViewForCrop.frame = CGRectMake(ratio,0,imageViewForCrop.frame.size.width,imageViewForCrop.frame.size.height);
            }
        }
        else if (image.size.width < image.size.height) // Height image
        {
            // after user pinch in the screen
            if (imageViewForCrop.frame.size.width > self.view.frame.size.width)
            {
                // allow dragging only in X coordinates by only updating the X coordenates with translation position
                recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y);
                [recognizer setTranslation:CGPointMake(0, 0) inView:imageViewForCrop];

                // get the top edge coordinate for the top left corner of crop frame
                float leftEdgeImagePosition = CGRectGetMinX(imageViewForCrop.frame);
                float leftEdgeCropEreaPosition = CGRectGetMinX(cropErea.frame);

                //get the buttom edge coordinate for bottom left corner of crop frame
                float rightEdgeImagePosition = CGRectGetMaxX(imageViewForCrop.frame);
                float rightEdgeCropEreaPosition = CGRectGetMaxX(cropErea.frame);
                //
                // if the top edge coordinate is less than or equal to Crop Erea frame
                if (leftEdgeImagePosition > leftEdgeCropEreaPosition)
                {
                    // draw drag view in max top position
                    imageViewForCrop.frame = CGRectMake(leftEdgeCropEreaPosition,imageViewForCrop.frame.origin.y,imageViewForCrop.frame.size.width,imageViewForCrop.frame.size.height);
                }
                // if bottom edge coordinate is greater than or equal to 480
                if (rightEdgeImagePosition < rightEdgeCropEreaPosition)
                {
                    float ratio = (imageViewForCrop.frame.size.width - cropErea.frame.size.width) * -1;
                    // draw drag view in max bottom position
                    imageViewForCrop.frame = CGRectMake(ratio,imageViewForCrop.frame.origin.y,imageViewForCrop.frame.size.width,imageViewForCrop.frame.size.height);
                }


            }

            // allow dragging only in Y coordinates by only updating the Y coordenates with translation position
            recognizer.view.center = CGPointMake(recognizer.view.center.x, recognizer.view.center.y + translation.y);

            [recognizer setTranslation:CGPointMake(0, 0) inView:imageViewForCrop];

            // get the top edge coordinate for the top left corner of crop frame
            float topEdgeImagePosition = CGRectGetMinY(imageViewForCrop.frame);
            float topEdgeCropEreaPosition = CGRectGetMinY(cropErea.frame);

            //get the buttom edge coordinate for bottom left corner of crop frame
            float bottomEdgeImagePosition = CGRectGetMaxY(imageViewForCrop.frame);
            float bottomEdgeCropEreaPosition = CGRectGetMaxY(cropErea.frame);
            //
            // if the top edge coordinate is less than or equal to Crop Erea frame
            if (topEdgeImagePosition > topEdgeCropEreaPosition)
            {
                // draw drag view in max top position
                imageViewForCrop.frame = CGRectMake(0,topEdgeCropEreaPosition,imageViewForCrop.frame.size.width,imageViewForCrop.frame.size.height);
            }
            // if bottom edge coordinate is greater than or equal to 480
            if (bottomEdgeImagePosition < bottomEdgeCropEreaPosition)
            {
                float ratio = (imageViewForCrop.frame.size.height - cropErea.frame.size.height) * -1;
                // draw drag view in max bottom position
                imageViewForCrop.frame = CGRectMake(0,ratio,imageViewForCrop.frame.size.width,imageViewForCrop.frame.size.height);
            }

        }
        else
        {
            imageViewForCrop.frame = CGRectMake(cropErea.frame.origin.x, cropErea.frame.origin.y, 320, 320);
        }
    }
    default:
        break;
}
}

这是捏法:

- (void)handlePinch:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
NSLog(@" X  %i " ,(int)imageViewForCrop.frame.origin.x );
if ((int)imageViewForCrop.frame.origin.x > 0 )
{
    [UIView animateWithDuration:0.4 animations:^
     {
         [imageViewForCrop setFrame:CGRectMake( 0 , imageViewForCrop.frame.origin.y , imageViewForCrop.frame.size.width , imageViewForCrop.frame.size.height )];
     }];


}
NSLog(@" Y  %i " , (int)imageViewForCrop.frame.origin.y );
if ((int)imageViewForCrop.frame.origin.y  > 0)
{
    [UIView animateWithDuration:0.4 animations:^
     {
         [imageViewForCrop setFrame:CGRectMake( imageViewForCrop.frame.origin.x , 0 , imageViewForCrop.frame.size.width , imageViewForCrop.frame.size.height )];
     }];



}
NSLog(@" Width  %i  and  %i " , (int)imageViewForCrop.frame.size.width , (int)originalSizeForIphoneWidth );
if ((int)imageViewForCrop.frame.size.width < (int)originalSizeForIphoneWidth)
{
    [UIView animateWithDuration:0.4 animations:^
     {
         [imageViewForCrop setFrame:CGRectMake( imageViewForCrop.frame.origin.x , imageViewForCrop.frame.origin.y , originalSizeForIphoneWidth+1, imageViewForCrop.frame.size.height)];
     }];


}
NSLog(@"Hight %i  and  %i " , (int)imageViewForCrop.frame.size.height , (int)originalSizeForIphoneHight );
if ((int)imageViewForCrop.frame.size.height <  (int)originalSizeForIphoneHight)
{

    [UIView animateWithDuration:0.4 animations:^
     {
         [imageViewForCrop setFrame:CGRectMake(imageViewForCrop.frame.origin.x , imageViewForCrop.frame.origin.y , imageViewForCrop.frame.size.width, originalSizeForIphoneHight+1)];
     }];

    //return;

    }

CGFloat factor = [(UIPinchGestureRecognizer *) pinchGestureRecognizer scale];
if(factor >1)
{
    // for the first time bug when pinch in or out
    if (lastScaleFactor == 0 )
    {
        lastScaleFactor = 1;
    }
    imageViewForCrop.transform =CGAffineTransformMakeScale(lastScaleFactor +(factor-1), lastScaleFactor +(factor-1));
}
else // pinch in
{
    imageViewForCrop.transform=CGAffineTransformMakeScale(lastScaleFactor * factor, lastScaleFactor*factor);

}

if (pinchGestureRecognizer.state == UIGestureRecognizerStateEnded)
{
    if (factor>1) {
        lastScaleFactor +=(factor-1);
    }
    else {
        lastScaleFactor*= factor;
    }
}
}

我用limitmaxOfTouches解决了这个问题:

// handle Pan
  UIPanGestureRecognizer *panOnScreen = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panOnScreen.maximumNumberOfTouches = 1;
[imageViewForCrop addGestureRecognizer:panOnScreen];

我希望这对某人有所帮助(-: .

最新更新