仅水平移动 UIView

发布于 2024-12-03 15:40:39 字数 809 浏览 0 评论 0原文

我有 2 个 UIView(placardView 和 PlacardView2),我想在触摸时限制它们水平移动,而不是使用 TouchMoved 方法垂直移动。我的想法是让 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;
   }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌伤ぢ 2024-12-10 15:40:40

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

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文