Created
May 1, 2015 14:41
-
-
Save t-kashima/f38812c9aeb0580aadae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env xcrun swift | |
import Foundation | |
import Cocoa | |
public class SelfTimer: NSObject { | |
let TimerInterval: NSTimeInterval = 3600 | |
var timer: NSTimer? | |
public typealias CallbackType = () -> Void | |
var callback: CallbackType? | |
// タイマーが動いているか | |
public func isRunning() -> Bool { | |
if self.timer != nil { | |
return self.timer!.valid | |
} | |
return false | |
} | |
// タイマーを動かす | |
public func start(#callback: CallbackType) { | |
// タイマーが終わったら呼び出されるコールバック | |
self.callback = callback | |
// 指定秒にタイマーを設定する | |
self.timer = NSTimer(timeInterval: self.TimerInterval, target: self, selector: "end", userInfo: nil, repeats: false) | |
// 別スレッドでタイマーを動かす | |
NSRunLoop.currentRunLoop().addTimer(self.timer!, forMode: NSRunLoopCommonModes) | |
} | |
// タイマーが止まった時 | |
func end() { | |
if self.callback != nil { | |
// コールバックを呼び出す | |
self.callback!() | |
} | |
} | |
} | |
// アラートを表示する | |
var showAlert = { () -> Void in | |
let alert = NSAlert() | |
alert.messageText = "立ちあがろう!!!" | |
alert.runModal() | |
} | |
var timer = SelfTimer() | |
timer.start(callback: showAlert) | |
// タイマーが有効な間はループを回す | |
let loop = NSRunLoop.currentRunLoop() | |
let futureDate = NSDate.distantFuture() as! NSDate | |
while timer.isRunning() && loop.runMode(NSDefaultRunLoopMode, beforeDate: futureDate) { | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment