- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在基于 Cocoa 的应用程序中,我有一个用于绘图的 Canvas ,它继承自 NSView,还有一个矩形,也继承自 NSView。在 Canvas 内部拖动矩形是没有问题的:
-(void)mouseDragged:(NSEvent *)theEvent {
NSPoint myOrigin = self.frame.origin;
[self setFrameOrigin:NSMakePoint(myOrigin.x + [theEvent deltaX],
myOrigin.y - [theEvent deltaY])];
}
像魅力一样工作。我现在遇到的问题:如何防止矩形移出 Canvas ?
所以,首先我想只为左边框修复这个问题,然后调整其他边缘。我的第一个想法是:“检查矩形的 x 原点是否为负数”。但是:一旦它为负,矩形就不能再四处移动(自然地)。我通过将矩形移动到 else 分支中的零 x 偏移来解决这个问题。这行得通,但是……很丑。
所以我对这个有点困惑,有什么提示吗?解决方案绝对是近在咫尺且简单易行。太简单了,我想不出来(一如既往的简单解决方案;)。
问候
苹果电脑
最佳答案
我建议不要使用 deltaX
和 deltaY
;尝试在 superview 中使用事件的位置。您将需要对 subview 的引用。
// In the superview
- (void)mouseDragged:(NSEvent *)event {
NSPoint mousePoint = [self convertPoint:[event locationInWindow]
fromView:nil];
// Could also add the width of the moving rectangle to this check
// to keep any part of it from going outside the superview
mousePoint.x = MAX(0, MIN(mousePoint.x, self.bounds.size.width));
mousePoint.y = MAX(0, MIN(mousePoint.y, self.bounds.size.height));
// position is a custom ivar that indicates the center of the object;
// you could also use frame.origin, but it looks nicer if objects are
// dragged from their centers
myMovingRectangle.position = mousePoint;
[self setNeedsDisplay:YES];
}
您将在 mouseUp:
中执行基本相同的边界检查。
更新:您还应该查看 View 编程指南,它会引导您创建可拖动 View :Creating a Custom View .
应该有用的示例代码,但与您的原始问题并不严格相关:
在 DotView.m 中:
- (void)drawRect:(NSRect)dirtyRect {
// Ignoring dirtyRect for simplicity
[[NSColor colorWithDeviceRed:0.85 green:0.8 blue:0.8 alpha:1] set];
NSRectFill([self bounds]);
// Dot is the custom shape class that can draw itself; see below
// dots is an NSMutableArray containing the shapes
for (Dot *dot in dots) {
[dot draw];
}
}
- (void)mouseDown:(NSEvent *)event {
NSPoint mousePoint = [self convertPoint:[event locationInWindow]
fromView:nil];
currMovingDot = [self clickedDotForPoint:mousePoint];
// Move the dot to the point to indicate that the user has
// successfully "grabbed" it
if( currMovingDot ) currMovingDot.position = mousePoint;
[self setNeedsDisplay:YES];
}
// -mouseDragged: already defined earlier in post
- (void)mouseUp:(NSEvent *)event {
if( !currMovingDot ) return;
NSPoint mousePoint = [self convertPoint:[event locationInWindow]
fromView:nil];
spot.x = MAX(0, MIN(mousePoint.x, self.bounds.size.width));
spot.y = MAX(0, MIN(mousePoint.y, self.bounds.size.height));
currMovingDot.position = mousePoint;
currMovingDot = nil;
[self setNeedsDisplay:YES];
}
- (Dot *)clickedDotForPoint:(NSPoint)point {
// DOT_NUCLEUS_RADIUS is the size of the
// dot's internal "handle"
for( Dot *dot in dots ){
if( (abs(dot.position.x - point.x) <= DOT_NUCLEUS_RADIUS) &&
(abs(dot.position.y - point.y) <= DOT_NUCLEUS_RADIUS)) {
return dot;
}
}
return nil;
}
点.h
#define DOT_NUCLEUS_RADIUS (5)
@interface Dot : NSObject {
NSPoint position;
}
@property (assign) NSPoint position;
- (void)draw;
@end
点.m
#import "Dot.h"
@implementation Dot
@synthesize position;
- (void)draw {
//!!!: Demo only: assume that focus is locked on a view.
NSColor *clr = [NSColor colorWithDeviceRed:0.3
green:0.2
blue:0.8
alpha:1];
// Draw a nice border
NSBezierPath *outerCirc;
outerCirc = [NSBezierPath bezierPathWithOvalInRect:
NSMakeRect(position.x - 23, position.y - 23, 46, 46)];
[clr set];
[outerCirc stroke];
[[clr colorWithAlphaComponent:0.7] set];
[outerCirc fill];
[clr set];
// Draw the "handle"
NSRect nucleusRect = NSMakeRect(position.x - DOT_NUCLEUS_RADIUS,
position.y - DOT_NUCLEUS_RADIUS,
DOT_NUCLEUS_RADIUS * 2,
DOT_NUCLEUS_RADIUS * 2);
[[NSBezierPath bezierPathWithOvalInRect:nucleusRect] fill];
}
@end
如您所见,Dot
类非常轻量级,并使用贝塞尔曲线路径进行绘制。 super View 可以处理用户交互。
关于objective-c - 移动 NSView 直到它碰到边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5744843/
我有一个 NSView 子类,可以在其 super View 中拖动。我通过在 mouseDragged 事件处理程序中调用 NSView 的 setFrameOrigin 和 setFrameRot
swift 3、iOS 10、macOS 10.12.4 我正在构建一个在 iOS 和 Mac 上运行的应用程序。在iOS端,我成功动画了一个UIView .当用户点击某物时,会出现一个弹出窗口并动画
所以,引用:Kiosk Applications - OS X programming - Multiple monitors 我有一个 800x600 NSView,当我使用 NSView 的 en
一旦我将自定义 NSView 嵌入到 ScrollView 中,我的自定义 View 中就不会绘制任何内容。 为了重现这个问题,我创建了一个 vanilla Cocoa 应用程序。我继承了 NSVie
我有一个 super View ,它有 2 个 subview 。这些 subview 是重叠的。 每当我从菜单中选择一个 View 时,相应的 View 应该成为前 View 并处理操作。即,它应该
说实话,我什至不知道从哪里开始。我觉得我已经在原地转了几个小时尝试不同的事情。 我的问题是如何在 IB 中配置 NSWindowControllers、NSViewControllers 和 NSVi
我有两个 NSViews 在彼此之上。一个 NSView 显示一个包含行的表格。单击一行时,另一个 View 会显示在该 View 的顶部。 现在的问题是,当我单击第二个 View 上的某个区域时,该
我有一个 NSView 的 subview (比如 MainView),里面有 NSTextField。 注意:此代码基于此 github.repo MainView 有如下指定的初始化器 init(
思路是这样的: 我有一个包含 2 个 NSView 的 NSWindow,我们称它们为 ViewA 和 ViewB。 ViewA 有一个 subview 对象列表,每个对象都有自己的跟踪区域集并处理一
我正在尝试使用从 nib 文件加载的新 View 来切换现有的 NSView。我发现有两种方法可以实现此目的 从 super View 中删除我的 subview 并将新的 subview 添加到 s
我有两个 NSView,a 和 b。具体来说,a是一个WebView,b是一个NSVisualEffectView,但我认为这并不重要. a 在非矩形 Alpha channel 中包含内容,我想使用
我有一个自定义 NSView(称为 Header),我可以在运行时加载它。然而,这个自定义 NSView (Header) 在运行时嵌入到另一个更大的自定义 NSView 中,并且它在底部产生额外的空
我目前正在尝试实现一些自定义 NSView,这些 NSView 将通过 macOS 应用程序中的某些事件进行动画处理。这些控件沿着您会在 AudioUnit 插件中找到的 slider 线。 关于动画
设置: 我的应用程序具有使用 enterFullScreenMode:withOptions: 设置为全屏模式的背景 View 。 对于这些背景 View ,我使用removeFromSupervie
更新:相对于下面的问题和答案,我似乎对 NSView 类与我试图绘制的自定义类有误解,包装NSScrollView。最后,我想要弄清楚的是如何在具有以下功能的 NSView 中管理自定义数据(不是照片
我有: NSStatusItem 带有自定义 View (在辅助线程中滚动文本),辅助线程更新内部状态并使用 setNeedsDisplay 通知主线程。 在 mouseDown 上,会弹出一个 NS
我正在做学习练习,同时为我的 child 制作一个游戏。他有一款纸牌游戏(比如《口袋妖怪》),我们扫描了一堆纸牌游戏,并试图制作一款他可以与“电脑”对战的游戏。所以游戏过程是你需要从选择你的牌开始。我
我有一个自定义的 NSView 子类,我想要它有圆角。我在 .m 文件中使用以下代码: #import "ItemImageSelectionView.h" @implementation ItemI
这是界面生成器的“对象”菜单中的内容 我很好奇 ImageView 、 PaintView 和 PlayerView 的顺序是否重要?就我而言,我想按以下顺序堆叠它们:PlayerView/Image
我有一个 MTKView,我正在其中渲染一个三角形,并且我希望能够使用调用 scrollWheel: 方法的两指拖动手势来平移它。 scrollWheel: 方法的默认行为是产生额外的滚动事件,以实现
我是一名优秀的程序员,十分优秀!