2011-09-07 95 views
0

我有2個UIViews(placardView和PlacardView2),我想限制他們時觸摸水平移動但不垂直使用touchesMoved方法。我的想法是讓Y座標等於自己,但是我不能在Xcode中使用語法,因爲我對這類事情很陌生。水平移動一個UIView

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 

    // If the touch was in the placardView, move the placardView to its location 
    if ([touch view] == placardView) { 
     CGPoint location = [touch locationInView:self]; 
     placardView.center = location; 
     placardView.center.y = placardView.center.y;//error is here 
     return; 
    } 
    if ([touch view] == placardView2) { 
     CGPoint location = [touch locationInView:self]; 
     placardView2.center = location; 
     placardView2.center.y = placardView2.center.y;//error is here 
     return; 
    } 
} 

回答

1

您不會通過將center.y分配給自己來改變任何東西 - 加上它已經被placardView2.center = location改變了;分配。相反,您應該只分配新的x座標:

CGPoint placardCenter = placardView2.center; 
placardCenter.x = location.x; 
placardView2.center = placardCenter; 
+0

謝謝SVD!看起來像這樣做後,我評論該行(placardView2.center =位置;)出並代之以你的代碼....我不知道爲什麼我有如此難以形象化。 :) –