开发者

Move a UIView only Horizontally

开发者 https://www.devze.com 2023-04-03 19:08 出处:网络
I have 2 UIViews (placardView, and PlacardView2) that I would like to constrain them when touched to move horizontally but not vertically using the touchesMoved Method. My thought was to make the Y开发

I have 2 UIViews (placardView, and PlacardView2) that I would like to constrain them when touched to move horizontally but not vertically using the touchesMoved Method. My thought was to make the Y开发者_开发知识库 co-ordinate equal to itself, but I cannot get that to work syntactically in Xcode as I am new to this sort of thing.

- (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;
   }
}


You won't change anything by assigning center.y to itself - plus it is already changed by placardView2.center = location; assignment. Instead, you should probably assign just the new x coordinate:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号