NSPopUpButton
का उप-वर्ग बनाएं और mouseDown
/mouseUp
ईवेंट ओवरराइड करें।
mouseDown
super
के कार्यान्वयन को कॉल करने से पहले एक पल के लिए ईवेंट देरी हो और केवल तभी माउस को दबाया जा रहा हो।
mouseUp
घटना बटन के target
/action
फायरिंग से पहले selectedMenuItem
nil
को (और इसलिए selectedMenuItemIndex
हो जाएगा -1
) निर्धारित किया है।
एकमात्र अन्य मुद्दा तेजी से क्लिक को संभालना है, जहां एक क्लिक के लिए टाइमर उस समय आग लग सकता है जब माउस कुछ भविष्य के क्लिक के लिए नीचे आ जाता है। NSTimer
का उपयोग करने और इसे अमान्य करने के बजाय, मैंने mouseDown
ईवेंट के लिए एक सरल काउंटर चुना है और काउंटर बदल गया है तो जमानत हो गई है।
// 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
स्रोत
2012-12-30 00:18:46
बहुत बुरा आप NSSegmentedControl के लिए ऊंचाई ऊंचाई सेट नहीं कर सकते - मुझे उस बटन को एक बड़े बटन से जुड़ा होना चाहिए। – zrxq
पूरी तरह से काम किया! धन्यवाद! – Alex
आईबी में चारों ओर खेलने के साथ अच्छी चाल, आप इस तरह एक वास्तव में सुरुचिपूर्ण नियंत्रण प्राप्त कर सकते हैं। –