拖动以调整大小的NSView(或其他对象) [英] Drag-to-resize NSView (or other object)

查看:73
本文介绍了拖动以调整大小的NSView(或其他对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个应用程序,该应用程序将允许用户使用可以调整大小的矩形边界框来指定图像的多个区域.

I'm trying to build an app that will allow the user to specify multiple areas of an image using rectangular bounding boxes that they can resize.

到目前为止,我有一个 NSScrollView 包含一个 NSImageView ,以便用户可以放大图像并根据需要滚动.我目前的想法是,我可以使用 NSViews 作为一种提供边框的方法,用户可以定位该边框并调整其大小以覆盖所需的区域,将 NSView 帧转换为百分比的图像大小,然后存储这些值以供以后使用.

So far, I've got an NSScrollView that contains an NSImageView so the user can zoom in on the image and scroll around as they desire. My current thinking is that I can use NSViews as a way to provide a bounding box that the user can position and resize to cover the desired area, convert the NSView frames into percentages of the image size, and then store those values for later use.

有一个 addAreaToImage 方法,可将 NSView 添加到用户当前所看位置的中心的 NSScrollView 中.我想要的是用户可以单击并拖动该区域的角以在所需位置调整大小/移动该区域.排序一个实时边界框,如果您愿意

There's an addAreaToImage method that adds an NSView to the NSScrollView at the center of wherever the user is currently looking. What I want is for the user to then be able to click and drag on the corners of the area to resize/move it wherever they want it to be. Sort of a live bounding box, if you will.

在阅读了文档之后,大多数与拖动有关的事情都是关于使NSView成为拖动其他物体(例如图像)或由于调整了Superview的大小而进行调整的地方,而这两个都不是我想要的要做.

After reading through the documentation, most of the things related to dragging are about making the NSView a place to drag something else (like an image) or resizing due to the superview being resized, neither of which are what I'm looking to do.

我担心这个问题的答案(或者使我能够提出自己的解决方案的答案集)是如此基础,以至于没人能想到它们,Googling的最后几天为我确认了很多.

My fear is that the answer to this problem (or the set of answers that would lead to me being able to roll my own solution) are so basic that no one ever thinks about them, which the last few days of Googling have pretty much confirmed for me.

我来自iOS开发,所以这并不是一个全新的领域,但是到目前为止,NSView和UIView似乎有足够的差异,使我彻底感到困惑.

I'm coming from iOS development, so this isn't completely new territory, but NSView and UIView seem to have enough differences to confuse me thoroughly so far.

推荐答案

是的,您将需要自己实现它,但并不太复杂.

Yes, you will need to implement it yourself but it isn't overly complicated.

首先,您需要对希望区域视图的行为和外观做出一些决策.您是否需要调整大小或也可以拖动(移动)视图?当它们处于被动/拖动/调整大小/突出显示状态时,如何绘制它们.您要调整大小并拖动光标吗?只是拖动一个角或所有边界,调整大小的行为是什么?拖动边框的宽度是多少?

First, you need to make some decisions about how you want your area views to behave and look like. Do you need just resize or be able to drag (move) the views as well? How are they drawn when they are passive/dragged/resized/highlighted. Do you want to have a resize and drag cursors? What is the behavior of the resizing, just drag a corner or all the borders? What's the drag border width?

然后将要用作区域视图的NSView子类化.给它一些私有成员以指示其状态(例如isDragged,isResized等).

You then subclass the NSView that you are using as your area views. Give it some private members to indicate its states (like isDragged, isResized etc).

实施 drawRect:绘制视图.考虑到它的各种状态(例如,您可能希望在拖动或调整其大小时可视化,绘制透明的覆盖层等).

Implement drawRect: to draw the view. Taking into account it various states (e.g you probably want to visualize when it is being dragged or resized, draw a transparent overlay, etc).

接下来,您要处理

Next you want to handle mouse events by implementing mouseDown:, mouseDragged:, mouseUp: and maybe mouseMoved:. Here your resize/drag logic will be placed. Check where the user initially clicked in mouseDown: and decide what operations are possible from that point setting the relevant states. Follow up in mouseDragged: to perform the operation (by setting the view's frame origin and size accordingly). Finalize the operation in mouseUp: (validate, set states, invoke done logic, register undo operation)

在处理点和矩形时,请不要忘记

When dealing with points and rects, don't forget about the coordinate system. You will need to translate them to/from views and base system. NSView has all the methods needed for this.

每次希望视图重绘以反映尺寸和位置的变化时,都需要调用 setNeedsDisplay: setNeedsDisplayInRect:.

You need to call setNeedsDisplay: or setNeedsDisplayInRect: each time you want the view to redraw itself to reflect the changes in size and position.

您可能还想使用跟踪区域用于视图中需要其他光标的区域(例如,角落处的调整大小的光标).

You may also want to use Tracking Areas for areas in your view that need a different cursor (e.g. resize cursor on the corner).

拖动/调整大小时,不要忘了实现逻辑以响应用户将鼠标拖出父级的视图范围.

When dragging/resizing don't forget to implement logic for responding to user dragging the mouse out of the parent's view bounds.

顺便说一句,为什么要将视图添加到滚动视图?我认为最好将它们作为imageview的子视图(如果可能)或clipview放置,以便可以滚动它们.

By the way, why are you adding your views to the scrollview? I think they are better placed as subviews of the imageview (if possible) or clipview so they can be scrolled.

这篇关于拖动以调整大小的NSView(或其他对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆