Showing posts with label NSTimer example. Show all posts
Showing posts with label NSTimer example. Show all posts

Saturday, April 27, 2013

NSTimer fireDate example ios


fireDate

Returns the date at which the receiver will fire.
- (NSDate *)fireDate
Return Value of [NSTimer fireDate]
The date at which the receiver will fire. If the timer is no longer valid, this method returns the last date at which the timer fired.
Discussion of [NSTimer fireDate]
Use the isValid method to verify that the timer is valid.
Example of [NSTimer fireDate]
-(void)fireCustomProfile:(NSTimer *)timer
{   
    if([[NSDate date] earlierDate:[schedTimer fireDate]])
    {
        NSLog(@"Ignoring Profile: %@",[schedTimer userInfo]);
        return;
    }

    notify_post("com.wrightscs.MobileProfiles.setCustomProfile");
}

NSTimer setFireDate example ios

setFireDate:
Resets the firing time of the receiver to the specified date.
- (void)setFireDate:(NSDate *)date
Parameters
date
The new date at which to fire the receiver. If the new date is in the past, this method sets the fire time to the current time.
Discussion of [NSTimer setFireDate]
You typically use this method to adjust the firing time of a repeating timer. Although resetting a timer’s next firing time is a relatively expensive operation, it may be more efficient in some situations. For example, you could use it in situations where you want to repeat an action multiple times in the future, but at irregular time intervals. Adjusting the firing time of a single timer would likely incur less expense than creating multiple timer objects, scheduling each one on a run loop, and then destroying them.[NSTimer setFireDate]
You should not call this method on a timer that has been invalidated, which includes non-repeating timers that have already fired. You could potentially call this method on a non-repeating timer that had not yet fired, although you should always do so from the thread to which the timer is attached to avoid potential race conditions.
Example of [NSTimer setFireDate]

-(IBAction)clicked:(id)sender
{
    if ([startStop.titleLabel.text isEqualToString:@"Start"]) 
    {
        [startStop setTitle:@"Pause" forState:UIControlStateNormal];
        [startStop setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
        timerDown = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];

    } else if ([startStop.titleLabel.text isEqualToString:@"Pause"])
    {
        [startStop setTitle:@"Resume" forState:UIControlStateNormal];
        [startStop setTitleColor:[UIColor colorWithRed:0/255 green:0/255 blue:255/255 alpha:1.0] forState:UIControlStateNormal];

        pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
        previousFireDate = [[timer fireDate] retain];
        [timer setFireDate:[NSDate distantFuture]];

    } else if ([startStop.titleLabel.text isEqualToString:@"Resume"])
    {
        [startStop setTitle:@"Pause" forState:UIControlStateNormal];
        [startStop setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

        float pauseTime = -1*[pauseStart timeIntervalSinceNow];
        [timer setFireDate:[NSDate dateWithTimeInterval:pauseTime sinceDate:previousFireDate]];
        [pauseStart release];
        [previousFireDate release];
    }
}

NSTimer initWithFireDate example ios


initWithFireDate :interval:target:selector:userInfo:repeats:

Initializes a new NSTimer object using the specified object and selector.
- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
Parameters
date
The time at which the timer should first fire.
seconds
For a repeating timer, this parameter contains the number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.[NSTimer initWithFireDate]
target
The object to which to send the message specified by aSelector when the timer fires. The target object is retained by the timer and released when the timer is invalidated.[NSTimer initWithFireDate]
aSelector
The message to send to target when the timer fires. The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
The timer passes itself as the argument to this method.
userInfo
Custom user info for the timer. The object you specify is retained by the timer and released when the timer is invalidated. This parameter may be nil.
repeats
If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
Return Value of [NSTimer initWithFireDate]
The receiver, initialized such that, when added to a run loop, it will fire at date and then, if repeats is YES, every secondsafter that.
Discussion of [NSTimer initWithFireDate]
You must add the new timer to a run loop, using addTimer:forMode:. Upon firing, the timer sends the messageaSelector to target. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)
Example of [NSTimer initWithFireDate]
NSDateComponents *comps = [[NSDateComponents alloc] init];

[comps setDay:24];
[comps setMonth:10];
[comps setYear:2011];

[comps setHour:23];
[comps setMinute:11];
[comps setSecond:0];


NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
schedulerTimer=[[NSTimer alloc] initWithFireDate:date interval:0 target:self selector:@selector(recordByScheduler:) userInfo:nil repeats:false];
[[NSRunLoop currentRunLoop] addTimer:schedulerTimer forMode:NSDefaultRunLoopMode];

NSTimer timerWithTimeInterval example ios


timerWithTimeInterval :invocation:repeats:

Creates and returns a new NSTimer object initialized with the specified invocation object.
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds invocation:(NSInvocation *)invocation repeats:(BOOL)repeats
Parameters
seconds
The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead
invocation
The invocation to use when the timer fires. The timer instructs the invocation object to retain its arguments.
repeats
If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
Return Value of [NSTimer timerWithTimeInterval]
A new NSTimer object, configured according to the specified parameters.
Discussion of [NSTimer timerWithTimeInterval]
You must add the new timer to a run loop, using addTimer:forMode:. Then, after seconds have elapsed, the timer fires, invoking invocation. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)
Example of [NSTimer timerWithTimeInterval]
NSTimer *timer = [NSTimer timerWithTimeInterval:1/3 target:self 
 selector:@selector(updateThingsOnce:) userInfo:nil repeats:YES];

[timer fire];

timerWithTimeInterval :target:selector:userInfo:repeats:

Creates and returns a new NSTimer object initialized with the specified object and selector.
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
Parameters
seconds
The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.
target
The object to which to send the message specified by aSelector when the timer fires. The target object is retained by the timer and released when the timer is invalidated.[NSTimer timerWithTimeInterval]
aSelector
The message to send to target when the timer fires. The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
The timer passes itself as the argument to this method.
userInfo
Custom user info for the timer. The object you specify is retained by the timer and released when the timer is invalidated. This parameter may be nil.
repeats
If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
Return Value of [NSTimer timerWithTimeInterval]
A new NSTimer object, configured according to the specified parameters.
Discussion of [NSTimer timerWithTimeInterval]
You must add the new timer to a run loop, using addTimer:forMode:. Then, after seconds seconds have elapsed, the timer fires, sending the message aSelector to target. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)
Example of [NSTimer timerWithTimeInterval]
{
NSLog(@"Connecting");
timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
[Connect setStringValue:@"a"]; 
connected = YES;
NSLog(@"Finished\n");
}

- (void)timerFireMethod:(NSTimer*)theTimer
{
    NSLog(@"Fireing event");
}