NSEvent clickCount ignore single click method if double click occurs - objective-c

Is it possible to ignore the first click if a double click is detected?
The following example will still log the one click even before the two clicks. I want to ignore the single click if there is a double click event.
- (void)mouseUp:(NSEvent *)theEvent {
if (theEvent.clickCount > 1) {
NSLog(#"two clicks %ld", theEvent.clickCount);
} else {
NSLog(#"one click %ld", theEvent.clickCount);
}
}
The purpose of doing this, is when the user single clicks in an NSView I want to perform a method that makes another view or window to appear. However, if the user double clicks inside the NSView then I want a different view or window to appear. The following steps outline this work flow:
user single clicks the NSView to make ViewA appear
user double clicks the NSView to make ViewB appear
The problem that I'm having is the single click is always detected therefore ViewA will always appear. But I do not want ViewA to appear if there is a double click, I just want ViewB to appear when there is a double click.

Unless, you can go back in time, you will need to cancel the previous single tap event somehow.
The easiest way: delay the execution on single tap event, and cancel it out if double click is detected.
[self performSelector:#selector(singleTap) withObject:nil afterDelay:[NSEvent doubleClickInterval]];
/// cancel out
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(singleTap) object:nil];
Or, you can execute single tap action immediately, and write a code to cancel it out if double click is detected. This should be very easy too. Because, if it's hard, there might be something wrong with your UX design.

Related

UISegmentedControl determine which button is tapped when momentary = YES

I have a UISegmentedControl element where there are two buttons (i.e "Delete all Points" and "Delete Pt #x"). I don't want the buttons to ever remain selected so I have them set as "momentary = YES". I also don't want to handle click events until AFTER the user has released a button. The problem is, I can't figure out which of the two buttons was pressed within the UISegmentedControl view.
What I've tried:
1) I've tried using the "ValueChanged" event. The nice thing here is I can get the selected index for which button was pressed (even when momentary=YES), but the problem is that this event if triggered when the user presses down... which I don't want. I want the event when the user releases their finger, and at that I only want the event when the user releases WITHIN the button view
2) I've tried using the "Touch Up Inside" event. But it doesn't trigger any events when I tap on one of the buttons...
Any help on this would be appreciated. (Programming in Objective-C)
I'm not sure what you mean when you say "I've tried using the "Touch Up Inside" event." Unless you specify precisely what you tried, with code, that is just a meaningless phrase.
Ideally, you'd want create a method something like the following, and set it up as the button's action:
- (IBAction)myButtonTouched:(id)sender forEvent:(UIEvent *)event {
if (event.type == UIControlEventTouchUpInside) {
// do your thing
}
}
Basically you send every user event for that button to the handler and test for the one you want.

Two buttons, one click

I have two buttons on top of each other in my nib. I need both of them to be pressed when tapped, but only the top button carries out its function. Is there a way for the top button to tell the bottom button to activate when the top one gets pressed. I do not think I can just merge the buttons into one and use one -(IBAction)buttonName function because one button is bigger than the other so I do not always need both to activate when one of them is pressed.
Thanks!
Is there a way for the top button to tell the bottom button to
activate when the top one gets pressed.
Not really, but you can have the action for the top button call the action for the bottom button.
Here's one way:
- (IBAction)actionTop:(id)sender
{
NSLog(#"The top button was activated.");
[self actionBottom:self];
}
- (IBAction)actionBottom:(id)sender
{
NSLog(#"The bottom button was activated.");
}
Another way would be to use the same action for both, and figure out what to do based on which button triggered the action:
- (IBAction)action:(id)sender
{
// if the top button was tapped, do this part
if (sender == self.topButton) {
NSLog(#"The top button was activated.");
}
// you want the bottom button to be activated no matter which button was tapped, so
// no need to check here...
NSLog(#"The bottom button was activated.");
}
the bottom button is the whole screen which changes what is displayed.
the top button plays a sound, except there are 4 top buttons that
plays different sounds
It seems like an invisible button covering the whole screen might be the wrong way to tackle the problem. You might look into using a gesture recognizer attached to your view to trigger the change. Your button actions could call the same method that the gesture recognizer uses.
Since one button is larger than the other, I assume that you would like the smaller button to "bring down" the larger button with it, including the change in the visual state. In cases like that you could send your target button a "tap" programmatically, like this:
- (IBAction)largeButtonClick:(id)sender {
}
- (IBAction)smallButtonClick:(id)sender {
// Perform the acton specific to only the small button, then call
[largeButton sendActionsForControlEvents:UIControlEventTouchUpInside];
}

Delay appears when I am trying to register double click event on NSTableView

I have NSTableView and NSView inside of this table using as cell. This NSView has 3 NSTextFields.
When I'm trying to select one of this NSTextFields they respond really quickly. But if I add some code to register double click event on NSTableView, delay appears. So when I click on textfield it thinks half a second and then activates it. But there is no such delay without double click event registered.
My code:
-(void)awakeFromNib{
[tasksList setDoubleAction:#selector(doubleClickInTable:)];
[tasksList setTarget:self];
}
-(void)doubleClickInTable:(id)sender
{
//Don't matter, it can be empty.
}

Check if spacebar is pressed inside a NSTextView

I have a NSTextView where I need to check when a specific button is pressed. Using ModifierKeyFlags I can check if buttons like shift and control is pressed, but I need to check if SPACE is pressed. Therefore, I cannot use the ModifierFlags (cause it can check some buttons, excluding the spacebar)
So I need a check, that notifies me everytime I press the spacebar in the textfield in my application. Any thoughts? I think it needs to be something like this:
if(spacebar is pressed) {
Dlog(#"give me a notification");
}
Use NSText's delegate.
-(void)textDidChange:(NSNotification *)notification
{
//Check if it has added a spacebar
}

Change default insertTab: action in NSSearchfield

I have a view with a nssearchfield a nstableview and a nsmatrix with three radiobuttons. Using delegates i change the selected radiobutton when the searchfield is the firstresponder and the user press tab, that works perfectly but what i want is that the searchfield don't loose the firstresponder when the user press tab
You can sub class NSSearchField and add this function
- (BOOL)resignFirstResponder {
return NO;
}
It will refuse to relinquish first responder status.
Another way is catch the windowDidUpdate notification. These are sent whenever anything changes, including change of focus, so you can check for the firstResponder and make it become first responder again.
[searchField becomeFirstResponder];