仅水平移动UIView

时间:2011-09-07 14:36:02

标签: cocoa-touch uiview move

我有2个UIViews(placardView和PlacardView2),我想在触摸时限制它们使用touchesMoved方法水平移动但不垂直移动。我的想法是让Y坐标等于它自己,但是我不能让它在Xcode中以语法形式工作,因为我是这类新手。

- (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 个答案:

答案 0 :(得分:1)

你不会通过将center.y分配给自己来改变任何东西 - 而且它已经被placardView2.center = location改变了;分配。相反,您应该只分配新的x坐标:

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