移动 NSView 直到它碰到边框
在基于 Cocoa 的应用程序中,我有一个用于绘图的画布,继承自 NSView,以及一个矩形,也继承自 NSView。在画布内部拖动矩形是没有问题的:
-(void)mouseDragged:(NSEvent *)theEvent {
NSPoint myOrigin = self.frame.origin;
[self setFrameOrigin:NSMakePoint(myOrigin.x + [theEvent deltaX],
myOrigin.y - [theEvent deltaY])];
}
就像魅力一样。我现在遇到的问题:如何防止矩形移出画布?
因此,首先我想仅针对左边框修复此问题,然后调整其他边缘。我的第一个想法是:“检查矩形的x原点是否为负”。但是:一旦为负数,矩形就不能再移动(自然地)。我通过将 else 分支中的矩形移动到零 x 偏移来解决这个问题。这可行,但它......丑陋。
所以我对这个有点困惑,有什么提示吗?毫无疑问,解决方案非常接近且简单。就这么简单,我无法弄清楚(就像往常一样,有简单的解决方案;)。
问候
Mac
In a Cocoa-based App i'm having a canvas for drawing, inherited from NSView, as well as a rectangle, also inherited from NSView. Dragging the rectangle around inside of the canvas is no problem:
-(void)mouseDragged:(NSEvent *)theEvent {
NSPoint myOrigin = self.frame.origin;
[self setFrameOrigin:NSMakePoint(myOrigin.x + [theEvent deltaX],
myOrigin.y - [theEvent deltaY])];
}
Works like a charm. The issue i'm having now: How can i prevent the rectangle from being moved outside the canvas?
So, first of all i would like to fix this just for the left border, adapting the other edges afterwards. My first idea is: "check whether the x-origin of the rectangle is negative". But: once it is negative the rectangle can't be moved anymore around (naturally). I solved this with moving the rectangle to zero x-offset in the else-branch. This works but it's ... ugly.
So i'm little puzzled with this one, any hints? Definitely the solution is really near and easy. That easy, that i cannot figure it out (as always with easy solutions ;).
Regards
Macs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议不要使用 deltaX 和 deltaY ;尝试使用超级视图中的事件位置。您需要对子视图的引用。
您将在
mouseUp:
中执行基本相同的边界检查。更新:您还应该查看视图编程指南,它将引导您创建可拖动视图:创建自定义视图。
示例代码应该会有所帮助,但与您原来的问题并不严格相关:
In DotView.m:
Dot.h
Dot.m
如您所见,
Dot
类非常轻量级,并使用贝塞尔曲线路径画画。超级视图可以处理用户交互。I'd suggest not using the
deltaX
anddeltaY
; try using the event's location in the superview. You'll need a reference to the subview.You'd do essentially the same bounds checking in
mouseUp:
.UPDATE: You should also have a look at the View Programming Guide, which walks you through creating a draggable view: Creating a Custom View.
Sample code that should be helpful, though not strictly relevant to your original question:
In DotView.m:
Dot.h
Dot.m
As you can see, the
Dot
class is very lightweight, and uses bezier paths to draw. The superview can handle the user interaction.