NSPopUpButton
의 하위 클래스를 만들고 mouseDown
/mouseUp
이벤트를 재정의하십시오.
의 구현을 호출하기 전과 마우스를 계속 누르고있는 경우에만 mouseDown
이벤트가 잠시 지연됩니다.
는
mouseUp
이벤트가 버튼의이
target
/
action
을 발사하기 전에
selectedMenuItem
(
-1
따라서
selectedMenuItemIndex
이 될 것입니다)
nil
에를 설정했습니다.
유일한 다른 문제는 빠른 클릭을 처리하는 것입니다. 여기서 빠른 클릭을 처리하는 것입니다. 즉, 향후 클릭을 위해 마우스를 눌렀을 때 한 번의 클릭에 대한 타이머가 작동 할 수 있습니다. NSTimer
을 사용하고이를 무효 화하는 대신에 나는 mouseDown
이벤트에 대한 간단한 카운터를 가지며 카운터가 변경된 경우 구제 조치를합니다. 사람이 여전히 필요하면 여기에 일반 NSButton 아닌 세그먼트 컨트롤을 기반으로 내 솔루션입니다,
// MyClickAndHoldPopUpButton.h
@interface MyClickAndHoldPopUpButton : NSPopUpButton
@end
// MyClickAndHoldPopUpButton.m
@interface MyClickAndHoldPopUpButton()
@property BOOL mouseIsDown;
@property BOOL menuWasShownForLastMouseDown;
@property int mouseDownUniquenessCounter;
@end
@implementation MyClickAndHoldPopUpButton
// highlight the button immediately but wait a moment before calling the super method (which will show our popup menu) if the mouse comes up
// in that moment, don't tell the super method about the mousedown at all.
- (void)mouseDown:(NSEvent *)theEvent
{
self.mouseIsDown = YES;
self.menuWasShownForLastMouseDown = NO;
self.mouseDownUniquenessCounter++;
int mouseDownUniquenessCounterCopy = self.mouseDownUniquenessCounter;
[self highlight:YES];
float delayInSeconds = [NSEvent doubleClickInterval];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if (self.mouseIsDown && mouseDownUniquenessCounterCopy == self.mouseDownUniquenessCounter) {
self.menuWasShownForLastMouseDown = YES;
[super mouseDown:theEvent];
}
});
}
// if the mouse was down for a short enough period to avoid showing a popup menu, fire our target/action with no selected menu item, then
// remove the button highlight.
- (void)mouseUp:(NSEvent *)theEvent
{
self.mouseIsDown = NO;
if (!self.menuWasShownForLastMouseDown) {
[self selectItem:nil];
[self sendAction:self.action to:self.target];
}
[self highlight:NO];
}
@end
너무 나쁘면 NSSegmentedControl의 사용자 높이를 설정할 수 없습니다. 큰 버튼에 첨부 된 메뉴가 필요합니다. – zrxq
완벽하게 일했습니다! 감사! – Alex
좋은 속임수로, IB에서 조금 놀아봤을 때 이렇게 우아한 컨트롤을 얻을 수 있습니다. –