在关于 Cocoa 绘图的最后一个问题的帮助下,我实现了一些基本形状,以及拖动/调整大小。
所以,现在我想弄清楚,如何在调整形状大小时创建类似 Keynote 的效果,它会自动适应旁边另一个形状的大小,然后“锁定”鼠标一点时间。
第一次尝试是使用延迟函数,比如
NSDate *future = [NSDate dateWithTimeIntervalSinceNow: 0.5 ];
[NSThread sleepUntilDate:future];
对所需事件使用react(例如形状宽度 == 高度)。但这不会产生预期的效果,因为整个应用程序会卡住指定的时间量。除此之外,我认为用户不会将其识别为“您已达到特殊尺寸”。仅在事件中显示指南不是解决方案,因为选择形状后会立即显示指南。
最佳答案
对于 snap to guides,我不认为你真的希望 cursor 停止。只是调整大小应该停止对目标的小范围内的光标移动使用react。
that other question 中的解决方案我认为或多或少是你想要的。本质上,当您离指南足够近时,只需将点的坐标更改为指南的坐标即可。因此,基于我在您之前的问题中发布的示例代码,这将成为您 View 的 mouseDragged:
和 mouseUp:
。如果您希望该点仅在鼠标松开时捕捉,您可以将新检查从 mouseDragged:
中移除,这是一种不同但同样有效的行为。
如果您要匹配矩形的边缘,您可能会找到 Foundation Rect Functions ,比如 NSMaxX
和 NSMaxY
,很有用。
- (void)mouseDragged:(NSEvent *)event {
if( !currMovingDot ) return;
NSPoint spot = [self convertPoint:[event locationInWindow]
fromView:nil];
spot.x = MAX(0, MIN(spot.x, self.bounds.size.width));
spot.y = MAX(0, MIN(spot.y, self.bounds.size.height));
// Look for Dots whose centerlines are close to
// the current mouse position
for( Dot * dot in dots ){
if (dot == currMovingDot) {
// Don't snap to myself! Leaving this out causes
// "snap to grid" effect.
continue;
}
// Where SNAP_DIST is #define'd somewhere
// something under 10 seems to be a good value
if( abs(spot.x - dot.position.x) <= SNAP_DIST ){
spot.x = dot.position.x;
}
if( abs(spot.y - dot.position.y) <= SNAP_DIST ){
spot.y = dot.position.y;
}
}
currMovingDot.position = spot;
[self setNeedsDisplay:YES];
}
关于objective-c - Cocoa 绘图, "lock"鼠标在特殊事件上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5844170/