gpt4 book ai didi

objective-c - 如何在透明 NSWindow 中区分 mouseDown 事件和 mouseDragged

转载 作者:行者123 更新时间:2023-12-04 18:19:54 26 4
gpt4 key购买 nike

我有一个透明的 NSWindow里面有一个简单的图标,可以在屏幕上拖动。
我的代码是:
。H:

@interface CustomView : NSWindow{
}

@property (assign) NSPoint initialLocation;

.m
@synthesize initialLocation;

- (id) initWithContentRect: (NSRect) contentRect
styleMask: (NSUInteger) aStyle
backing: (NSBackingStoreType) bufferingType
defer: (BOOL) flag{
if (![super initWithContentRect: contentRect
styleMask: NSBorderlessWindowMask
backing: bufferingType
defer: flag]) return nil;
[self setBackgroundColor: [NSColor clearColor]];
[self setOpaque:NO];
[NSApp activateIgnoringOtherApps:YES];
return self;
}

- (void)mouseDragged:(NSEvent *)theEvent {
NSRect screenVisibleFrame = [[NSScreen mainScreen] visibleFrame];
NSRect windowFrame = [self frame];
NSPoint newOrigin = windowFrame.origin;

// Get the mouse location in window coordinates.
NSPoint currentLocation = [theEvent locationInWindow];
// Update the origin with the difference between the new mouse location and the old mouse location.
newOrigin.x += (currentLocation.x - initialLocation.x);
newOrigin.y += (currentLocation.y - initialLocation.y);

// Don't let window get dragged up under the menu bar
if ((newOrigin.y + windowFrame.size.height) > (screenVisibleFrame.origin.y + screenVisibleFrame.size.height)) {
newOrigin.y = screenVisibleFrame.origin.y + (screenVisibleFrame.size.height - windowFrame.size.height);
}

// Move the window to the new location
[self setFrameOrigin:newOrigin];
}

- (void)mouseDown:(NSEvent *)theEvent {
// Get the mouse location in window coordinates.
self.initialLocation = [theEvent locationInWindow];
}

我想显示 NSPopover当用户单击透明窗口的图像时。但是,正如您在代码中看到的, mouseDown事件用于获取鼠标位置(上面的代码取自示例)。
当用户单击图标只是为了拖动它或只是单击它以显示 NSPopover 时,我该怎么做才能知道? ?
谢谢

最佳答案

这是在您需要它以开始操作之后接收定义事件的经典情况。具体来说,直到拖动开始之后,您才能知道 mouseDown 是否是拖动的开始。但是,如果没有开始拖动,您希望对该 mouseDown 采取行动。

在 iOS 中(我意识到这与这里的代码没有直接关系,但它是指导性的),有一个完整的 API 围绕着让 iOS 尝试为您做出这些决定。整个手势系统是基于这样一种想法,即用户开始做某事可能是许多不同 Action 之一,因此需要随着时间的推移而解决,可能导致在跟踪期间取消 Action 。

在 OS X 上,我们没有很多系统可以帮助解决这个问题,所以如果你有一些需要不同地处理点击和拖动的东西,你需要推迟你的下一个 Action ,直到一个保护时间过去,如果通过,您可以执行原始操作。在这种情况下,您可能需要执行以下操作:

mouseDown ,开始 NSTimer设置一个适当的保护时间(不要太长以至于人们会不小心移动指针,也不要太短以至于你会在用户拖动之前触发)以便稍后给你回电以触发弹出框。

mouseDragged , 使用警戒区域来确保如果用户只是稍微抽搐一下,就不算拖拽。这可能很烦人,因为有时需要将某些东西拖得比开始拖动所需的距离要远,因此您需要在某个地方找到一个神奇的常数,或者进行一些实验。当超出警戒区域时,通过取消 NSTimer 开始您的合法拖动操作。与 [timer invalidate]做你的阻力。

在计时器的回调中,显示您的弹出框。如果用户拖动,NSTimer将已失效,导致它不会触发,因此不会显示弹出框。

关于objective-c - 如何在透明 NSWindow 中区分 mouseDown 事件和 mouseDragged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10888065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com