• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Re: Action while trcking, Different action on mouseUp
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Re: Action while trcking, Different action on mouseUp


  • Subject: Re: Re: Action while trcking, Different action on mouseUp
  • From: "Shawn Erickson" <email@hidden>
  • Date: Thu, 17 Aug 2006 10:08:19 -0700

On 8/17/06, Matt Neuburg <email@hidden> wrote:
On Wed, 16 Aug 2006 15:43:45 -0400, "Michael Ash" <email@hidden>
said:
>On 8/16/06, Matt Neuburg <email@hidden> wrote:
>> On Mon, 14 Aug 2006 22:44:27 +0000, Trygve Inda <email@hidden>
>> said:
>> >I have an NSSlider that has an action (through a binding) to display the
>> >current slider value while tracking. I'd like a different action to call a
>> >method when the user releases the mouse to do stuff with the new setting.
>> >
>> >How can I enable this?
>>
>> That's truly silly. How do you know the user will use the mouse to slide the
>> slider? It's perfectly possible to use the keyboard instead. m.
>
>It's not that silly. Often a slider manipulates something that is
>intensive enough to recompute as to interfere with smooth mouse
>tracking. In the keyboard case you just swallow the cost and update
>all the time.
>
>Fortunately it's easy to implement. Write your action like so:
>
>- (void)sliderMoved: (id)sender {
>   ... stuff you want to do with every new value, if any ...
>
>   [NSObject cancelPreviousPerformRequestsWithTarget: self selector:
>@selector( sliderCommit: ) object: nil];
>   [self performSelector: @selector( sliderCommit: ) withObject: nil
>afterDelay: 0.0];
>}
>
>- (void)sliderCommit: (id)ignore {
>   ... this gets executed immediately after any keystroke or mouseup ...
>}
>
>The key here is that delayed performs, by default, are only executed
>in the default runloop mode, but control tracking has a separate
>runloop mode. You cancel old ones to make sure the commit only happens
>once, and it gets executed at the end.

I didn't say that doing something when there is a relatively long pause in
the slider movement was silly. That is what I was implying the OP *should*
do. What I said was silly was to tie this to mouseUp, as if the latter were
guaranteed.

Here's an approach that covers both mouse movement and keyboard movement of
the slider:

- (IBAction)sliderAction:(id)sender {
    NSLog(@"do intermediate stuff");
    if (self->timer)
        [self->timer invalidate];
    self->timer = [NSTimer scheduledTimerWithTimeInterval:0.6 target:self
                                       selector:@selector(sliderDone:)
                                       userInfo:nil repeats:NO];
}

- (void) sliderDone: (id) dummy {
    self->timer = nil;
    NSLog(@"do final stuff");
}

I would drop the use of NSTimer in the above, for example...

@implementation NSObject (MyUtils)
- (void) cancelDelayedPerformSelector:(SEL)aSelector withObject:(id)anArgument {
   [NSObject cancelPreviousPerformRequestsWithTarget:self
selector:aSelector object:anArgument];
}
@end

- (IBAction) sliderAction:(id)sender {
   NSLog(@"do intermediate stuff");

   SEL selector = @selector(sliderDone:);
   [self cancelDelayedPerformSelector:selector withObject:nil];
   [self performSelector:selector withObject:self afterDelay:<some delay>];
}

- (void) sliderDone:(id)sender {
   NSLog(@"do final stuff");
}

-Shawn
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: Action while trcking, Different action on mouseUp (From: Matt Neuburg <email@hidden>)

  • Prev by Date: Re: NSTableView w/Contextual Menu
  • Next by Date: Re: Compiling a 10.4 Project for 10.3.9
  • Previous by thread: Re: Action while trcking, Different action on mouseUp
  • Next by thread: Garbage collection in 10.5
  • Index(es):
    • Date
    • Thread