在同一个UIImageView上处理/检测UIPanGestureRecognisor和UISwipeGestureRecognisor

时间:2014-07-15 12:08:48

标签: ios objective-c uigesturerecognizer uipangesturerecognizer uiswipegesturerecognizer

我有一个图片库,如果用户单击一张图片,它会全屏显示ImageView,

现在如果用户使用UIPinchGesture图片完全放大/缩小。

如果图片没有缩放我想要实现UISwipeGesture应该工作并且在Swipe Gesture上调用图库中的下一个图像。

如果图片被缩放,则swipeGesture停止工作,UIPanGesture应该可以正常工作。

实现它的最佳方法是什么,

我是这样做的。但总是混合PanGesture和SwipeGesture。

我的滑动处理方法

- (void)handleSwipe:(UIGestureRecognizer *)sender
{
    if (stopSwipe == true) // bool iVar (stopSwipe)
    {
        return;
    }
    UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];
    switch (direction)
    {
        case UISwipeGestureRecognizerDirectionLeft:
            newIndexPath = [NSIndexPath indexPathForRow:myIndexPath.row+1 inSection:myIndexPath.section];
            [self ChangeImage];
            myIndexPath = [NSIndexPath indexPathForRow:myIndexPath.row+1 inSection:myIndexPath.section];
            break;
        case UISwipeGestureRecognizerDirectionRight:
            newIndexPath = [NSIndexPath indexPathForRow:myIndexPath.row-1 inSection:myIndexPath.section];
            [self ChangeImage];
            myIndexPath = [NSIndexPath indexPathForRow:myIndexPath.row-1 inSection:myIndexPath.section];
            break;
        default:
            break;
    }
}

我的捏合处理方法

-(void)handlePinch:(UIPinchGestureRecognizer*)sender
{
static CGRect initialBounds;
if (sender.state == UIGestureRecognizerStateBegan)
{
    initialBounds = myLrgImageView.bounds;
}
CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];

CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformIdentity, factor, factor);
myLrgImageView.bounds = CGRectApplyAffineTransform(initialBounds, transform);
}

我的平底锅处理方法

-(void)handlePan:(UIPanGestureRecognizer*)recognizer
{
    //after pinch 'myLrgImageView.frame' is changed.
    //imgActualBoundry is the normal size with out zooming.
    if ([NSValue valueWithCGRect:myLrgImageView.frame] > [NSValue valueWithCGRect:imgActualBoundry])
    {
        stopSwipe = true;
        CGPoint translation = [recognizer translationInView:self.myLrgImageView];
        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
       [recognizer setTranslation:CGPointMake(0, 0) inView:self.myLrgImageView];

       if (recognizer.state == UIGestureRecognizerStateEnded)
       {
           CGPoint velocity = [recognizer velocityInView:self.myLrgImageView];
           CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
           CGFloat slideMult = magnitude / 200;
           NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);

           float slideFactor = 0.1 * slideMult; // Increase for more of a slide
           CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
                                         recognizer.view.center.y + (velocity.y * slideFactor));
           finalPoint.x = MIN(MAX(finalPoint.x, 0), self.myLrgImageView.bounds.size.width);
           finalPoint.y = MIN(MAX(finalPoint.y, 0), self.myLrgImageView.bounds.size.height);
        }
    }
    else
    {
        stopSwipe = false;
    }
}

我曾尝试使用或不使用这种同时使用多种手势的方法,但没有取得成功。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

1 个答案:

答案 0 :(得分:1)

我从您的问题中得出的结论是:您希望在相同的UIImageView上使用平移和滑动手势,但具体情况如下。

如果我说的只是一个懒惰的人,只需先在iVar中获取图像的实际大小然后检查两个委托方法:

if size is changed then you will surely like to use Pan gesture on same Image.
&
if size is the same as actual image then Swipe. 

我只是想发布你的想法。

很抱歉发布不是代码,实际上面临电力短缺和笔记本电池即将死机。 :P