从屏幕外防止UIPanGesture并超出其父视图的界限 - iOS

时间:2014-04-25 14:53:35

标签: ios uigesturerecognizer uipangesturerecognizer

我猜它是重复的问题,但我已经检查了有关stackoverflow的所有信息,我无法弄明白。

zooming的图片中UICollectionViewCell后,我想平移图片,但只到UIScreen /父视图的边框。

这就是发生的事情:

enter image description here

这是我用于Scaling&的代码。 Pan

- (void)moveImage:(UIPanGestureRecognizer *)recognizer
{
    if (self.pinchGesture.scale<1.0){
        return;
    }

    if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged) {

        CGPoint translation = [recognizer translationInView:self.superview];

        CGPoint newcenter = CGPointMake(self.center.x + translation.x, self.center.y + translation.y);

         [self setCenter:newcenter];
         [recognizer setTranslation:CGPointZero inView:self];      
    }

}


- (void)doPinch:(UIPinchGestureRecognizer *)gesture
{ 
    if([gesture state] == UIGestureRecognizerStateBegan) {

        previousScale = 1.0;

    }

    CGFloat currentScale = [[self.layer valueForKeyPath:@"transform.scale"] floatValue];

    // Constants to adjust the max/min values of zoom
    const CGFloat kMaxScale = 4.0;
    const CGFloat kMinScale = 1.0;

    CGFloat newScale = 1 -  (previousScale - [gesture scale]); // new scale is in the range (0-1)
    newScale = MIN(newScale, kMaxScale / currentScale);
    newScale = MAX(newScale, kMinScale / currentScale);

    self.transform = CGAffineTransformScale(self.transform, newScale, newScale);

}

提前致谢;)

1 个答案:

答案 0 :(得分:1)

最后我使用实现UIScrollView的缩放。我在UIScrollView添加了UICollectionViewCell

我已将其设置为minimumZoomScale的{​​{1}},maximumZoomScalezoomScale,并实施了UIScrollView viewForZoomingInScrollView方法在我的自定义UIScrollViewDelegate上,以便在UICollectionViewCell中运行zoom功能。

这就是我UIScrollView的{​​{1}}:方法:

cellForItemAtIndexPath