4

我正在研究 pdf 页面上的捏合和捏出功能。我的捏合和平移(移动)工作正常,但是当用户连续移动缩放视图时,缩放视图超出了超级视图边界。像这样:

这是用户在缩放后所做的移动

我如何限制平移移动,以便用户无法将缩放视图/pdf 移动到超级视图之外。
我正在使用的相关代码是:

// This method will handle the PINCH / ZOOM gesture 

- (void)pinchZoom:(UIPinchGestureRecognizer *)gestureRecognizer {

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        // Reset the last scale, necessary if there are multiple objects with different scales
        lastScale = [gestureRecognizer scale];
    }
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

    if (!zoomActive) {
        zoomActive = YES;
        panActive = YES;
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panMove:)];
        [panGesture setMaximumNumberOfTouches:2];
        [panGesture setDelegate:self];
        [self addGestureRecognizer:panGesture];
        [panGesture release];

    }

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

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

            CGFloat newScale = 1 -  (lastScale - [gestureRecognizer scale]); 
            newScale = MIN(newScale, kMaxScale / currentScale);   
            newScale = MAX(newScale, kMinScale / currentScale);
            CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
            [gestureRecognizer view].transform = transform;

            lastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call 



    [delegate leavesView:self zoomingCurrentView:[gestureRecognizer scale]];            




}


我处理平移移动的方法 :

// This method will handle the PAN / MOVE gesture 
- (void)panMove:(UIPanGestureRecognizer *)gestureRecognizer  
{  
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged)   {  
        CGPoint translation = [gestureRecognizer translationInView:[[gestureRecognizer view] superview]];  
        [[gestureRecognizer view] setCenter:CGPointMake([[gestureRecognizer view] center].x + translation.x, [[gestureRecognizer view] center].y + translation.y)];  

        [gestureRecognizer setTranslation:CGPointZero inView:[[gestureRecognizer view] superview]];  
    }  
}  

请建议如何在其超级视图范围内处理平移/移动限制平移。

4

2 回答 2

3

panMove在您的方法中尝试此代码。在我的情况下它工作正常。

        static CGPoint initialCenter;

        if (recognizer.state == UIGestureRecognizerStateBegan)
        {
            initialCenter = recognizer.view.center;
        }

        CGPoint translation = [recognizer translationInView:recognizer.view];
        CGPoint newCenter = CGPointMake(initialCenter.x + translation.x,
                                       initialCenter.y + translation.y);
        CGRect newFrame = recognizer.view.frame;
        CGRect superViewBounds = recognizer.view.superview.bounds;
        CGPoint superViewOrigin = recognizer.view.superview.frame.origin;

        if(newCenter.x-(newFrame.size.width/2) >= (superViewBounds.size.width+superViewOrigin.x)-200 /*right*/
           ||
           newCenter.x+(newFrame.size.width/2) <= (superViewOrigin.x+200) /*left*/
           ||
           newCenter.y-(newFrame.size.height/2) >= (superViewBounds.size.height+superViewOrigin.y)-200 /*bottom*/
            ||
            newCenter.y+(newFrame.size.height/2) <= (superViewOrigin.y+100)) /*top*/
        {
            return;
        }else{
            recognizer.view.center = newCenter;
        }
于 2014-01-28T05:15:29.783 回答
0

您可以在代码中添加类似这样的内容:(这有点粗糙,因此您可能需要解决一些错误)

if([gestureRecognizer view].frame.bounds.x < self.view.bounds.x - panExapantion)
{ 
    //then don't move it
}

//... repeat this for all sides (this is left), bottom, right, and top.

编辑:好的,考虑一个盒子里面的盒子,因此我们有一个内盒和一个外盒。如果我们不希望内盒超出外盒,那么我们必须让所有这些陈述都为真:

  1. 被移动的内箱左侧不在外箱左侧之外。
  2. 被移动的内盒右侧不在外盒右侧之外。
  3. 被移动的内盒底边不在外盒底边之外。
  4. 内盒的移动顶面不在外盒的顶面之外。

在您的情况下,PDF 是内盒,iPad 是外盒。为了阻止 pdf 超出框外,我们需要检查这些陈述中的每一个是否为真,如果其中一个为假,我们不会将 PDF 移动到它的新位置,或者我们将 PDF 移动到 iPhone 边缘附近屏幕。

问题是如果使用捏合和缩放,那么突然盒子总是在外框之外,那么我们该如何解决呢?我们得到了当它被放大时有多少像素被添加到里面的盒子里(为了解释这个问题,让我们称之为扩展)。所以我们得到了盒子扩大了多少,然后减去了那个值。像这样:(这是一个愚蠢的 if 语句,在代码中不起作用)

If(outerBox.leftSide is less than innerBox.leftSide - panExpantion) 
{
    //Then the innerBox is outside the outterBox
}

我希望这有助于澄清!

于 2012-06-06T13:22:52.237 回答