user3708335
user3708335

Reputation: 469

Making Timer (swift) without NSTimer

Is it possible to make a timer in Xcode 6 without using a NSTimer? By this I mean you can specify a time increment to repeat a certain amount of code? Or to add on is it possible to make a NSTimer that isn't has a selector selecting a different method just continues the code inside the same method the NSTimer is implement in?

Upvotes: 0

Views: 559

Answers (3)

Albert Renshaw
Albert Renshaw

Reputation: 17902

Sure! I have an objective-c macro I made for this purpose that will likely port to swift in some way or another.

#define startBlockTimer(delayInSeconds, block) {\
__block float runTime = (-1.0f*delayInSeconds);\
__block BOOL keepRunning = YES;\
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ for(;keepRunning&&(runTime+=delayInSeconds);[NSThread sleepForTimeInterval:delayInSeconds]) {dispatch_async(dispatch_get_main_queue(), block );}});\
}
#define blockTimerRunTime runTime
#define stopBlockTimer() keepRunning = NO;

Used like this:

startBlockTimer(0.5, ^{

        self.view.backgroundColor = [UIColor colorWithHue:arc4random_uniform(1000)/1000.0f saturation:0.75f brightness:0.75f alpha:1.0f];

        if (blockTimerRunTime > 5.0f) {
            stopBlockTimer();
        }

    });

Upvotes: 0

vladof81
vladof81

Reputation: 26509

It is possible to build a scenario as described in your post. Following code shows the basic idea of what I would do to simulate a timer without NSTimer. Note, by default, the code is using NSThread, alternatively you may set useGCD true to dispatch using GCD.

class Timer: NSObject {

    var interval = 1.0 // Interval at 1.0 second
    var useGCD = false // Set true to use GCD

    var _isTimerRunning = false

    func start() {
        if !_isTimerRunning {
            if !useGCD {
                var thread = NSThread(target: self, selector: Selector("timerFunction"), object: nil)
                thread.start()
            } else {
                var queue = dispatch_queue_create("com.example.threading", nil)
                dispatch_async(queue, {
                    self.timerFunction()
                })
            }
            _isTimerRunning = true
        }
    }

    func stop() {
        _isTimerRunning = false
    }

    func timerFunction() {
        while (_isTimerRunning) {
            /*
             * TO-DO Designated code goes here
             */
            NSThread.sleepForTimeInterval(interval) // Interrupt
        }
    }
}

Start timer:

var timer = Timer()
timer.start()

Regards

Upvotes: 1

Steve Rosenberg
Steve Rosenberg

Reputation: 19524

Can you use a delay function? Set inside a loop to fire repeatedly.

delay (5.0) {

//code to execute here

}

func delay(delay:Double, closure:()->()) {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure)
    }

Upvotes: 0

Related Questions