2012-11-07 192 views
0

在我的應用程序中,我有一個UIview,其中包含多個uiview作爲子視圖。我想拖動一個單獨的UIView在直線水平,我用SetCenter屬性,但它不工作。 我的代碼:沿線路徑拖動uiview

CGPoint translatedPoint = [gestureRecognizer translationInView:[myview superview]]; 

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) { 
     _firstX = [myview center].x; 
     _firstY = [myview center].y; 
    } 

    translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y); 

    [myview setCenter:translatedPoint]; 

這裏時,我開始拖動它與我的鼠標指針位置移動沿着myview。但是我只想在HORIZONTAL路徑中拖動(即直線)。

回答

0
CGPoint translatedPoint = [gestureRecognizer translationInView:[myview superview]]; 

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) { 
     _firstX = [myview center].x; 
    } 

    translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY); 

    [myview setCenter:translatedPoint]; 
+0

_firstY的值是多少? –

+0

當你定義它時,你還沒有給變量_firstY賦值嗎? – Kassem

3

所以只需更新x座標而不是y。