2

我有一個UIViewController,它調用一個UIView:的iOS - 設置最小/最大限制平移

我使用UIPinchGesture放大到UIView的 我想要做的就是限制用戶多少可以平移,根據縮放比例

即 「currentScale」

目前我使用的代碼允許無平移,當currentScale(金額放大)小於1.1倍變焦,但如果它是偉大的,1.1它允許pannin,但這允許UIView被平移和移動無邊界,我希望能夠et panning amount to its boundaries:當前代碼

if (currentScale <= 1.1f) { 
    // Use this to animate the position of your view to where you want 
    [UIView animateWithDuration: 0.5 
          delay: 0 
         options: UIViewAnimationOptionCurveEaseOut 
        animations:^{ 
         CGPoint finalPoint = CGPointMake(self.view.bounds.size.width/2, 
                  self.view.bounds.size.height/2); 
         recognizer.view.center = finalPoint; } 
        completion:nil]; 
} 

else { 
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
             recognizer.view.center.y + translation.y); 
    [recognizer setTranslation:CGPointZero inView:self.view]; 
} 

某些方向,將非常感激 - 謝謝!

+0

您正在使用平移還是捏手勢? –

+1

我認爲這可能有助於你。它對我來說很有用。 http://stackoverflow.com/questions/1362718/scroll-a-background-in-a-different-speed-on-a-uiscrollview –

+0

@pratyusha - 我使用兩個,Rajpuroht - 這是爲了UIScrollView:討論速度,而不是限制空間 –

回答

1

免責聲明 - 這可能不是這樣做的最佳方式,但是這是我如何解決它:

1)我推斷我是多麼需要在5個不同的X 0R Y方向平移通過測量視圖中心偏離其原始位置的方式來確定縮放點:

2)我使用NSLog進行大部分測量) - 我對結果進行了標準化 - 並將其繪製在excel中 - 繪製出曲線 - 並得到一個公式縮放級別Vs View.center

3)然後我簡單地編碼平移手勢根據我得到的公式:

的代碼如下(XMAX,XMIN,YMAX,YMIN都已經繪製方程作爲「zoomScale」

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

//dont pan if zoomscale = 1 (this indicates no zooming) 
if (zoomScale <= 1.0f) { 
    return; 
} 

//panning gesture began/state changes 
if ([recognizer state] == UIGestureRecognizerStateBegan || 
    [recognizer state] == UIGestureRecognizerStateChanged) { 

    //detect translation gesture 
    translation = [recognizer translationInView:self.view]; 
    //newCenter is a variable detecting how your translation gesture would efect your view's center 
    CGPoint newCenter = CGPointMake(recognizer.view.center.x + translation.x, 
            recognizer.view.center.y + translation.y); 

    //Check whether boundary conditions are met 
    BOOL inBounds = (newCenter.y >= yMin && newCenter.y <= yMax && 
        newCenter.x >= xMin && newCenter.x <= xMax); 

    if (inBounds) { 
     //if boundary conditions met : translate your view 
     recognizer.view.center = newCenter; 
     [recognizer setTranslation:CGPointZero inView:self.view]; 
    } 
} 

希望這能幫助別人在那裏的共同因素:不,你必須聲明你必須在您的viewDidLoad方法中啓動(聲明)UIPanGestureRecognizer以使其工作

相關問題