仅水平移动 UIView
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不会通过将 center.y 分配给自身来更改任何内容 - 而且它已经被 placardView2.center = location; 更改了任务。相反,您应该只分配新的 x 坐标:
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: