Timer:"), userInfo: nil, repeats: true) }else { timer?.invalidate() timer = nil } setSettingButtonsEnabled(!newValue) } } //计时方法每一秒调用改变timeLable的显示,直到计时结束
func updateTimer(timer:NSTimer){ remainingSeconds--
if remainingSeconds<=0 { isCounting = false var alertVC = UIAlertController(title: "时间到", message: "", preferredStyle:.Alert) let action = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: { (action) -> Void in println("被按了") }) alertVC.addAction(action) self.presentViewController(alertVC, animated: true, completion: nil) } } //打开或关闭设置时间的按钮
func setSettingButtonsEnabled(enable:Bool) { for button in timeButtons { button.enabled = enable button.alpha = enable ?1.0 : 0.3 } resetButton.enabled = enable resetButton.alpha = enable ?1.0 : 0.3 let title = enable ? "启动" : "停止" startButton.setTitle(title, forState: .Normal) } 此时再实现startButtonTapped:和resetButtonTapped:方法,代码如下所示: func startButtonTapped(button:UIButton){ isCounting = !isCounting } func resetButtonTapped(){ remainingSeconds = 0 } 步骤四:后台计时,推送消息 IOS8对程序的后台运行有很好的支持,当计时器退到后台,不用做任何操作就会自动后台计时,但是当计时完成用户并不知道计时已经完成,所以这个时候就需要使用IOS系统提供的本地消息推送功能,首先需要在程序加载成功的方法中请求用户同意接受消息推送,代码如下所示: func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds) //可选链,设置背景颜色
self.window?.backgroundColor = UIColor.whiteColor() self.window?.rootViewController = ViewController() self.window?.makeKeyAndVisible() //请求用户获取消息推送权限
let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Sound | UIUserNotificationType.Badge,categories: nil) application.registerUserNotificationSettings(settings) return true } 其次从计时开始就添加消息推送,当计时完成时推送消息,代码如下所示: func startButtonTapped(button:UIButton){ isCounting = !isCounting if isCounting { //添加消息推送
createAndFireLocalNotificationAfterSeconds(Double(remainingSeconds)) }else { //计时结束退出消息推送
UIApplication.sharedApplication().cancelAllLocalNotifications() } } //添加消息推送方法
func createAndFireLocalNotificationAfterSeconds(seconds:NSTimeInterval) { UIApplication.sharedApplication().cancelAllLocalNotifications() let notification = UILocalNotification() //推送时间
notification.fireDate = NSDate(timeIntervalSinceNow: seconds) //推送时区
notification.timeZone = NSTimeZone.systemTimeZone() //推送通知的内容
notification.alertBody = "Time is up!" UIApplication.sharedApplication().scheduleLocalNotification(notification) } 运行程序消息推送效果如图-10所示: 图-10
3.4 完整代码 本案例中,AppDelegate.swift文件中完整代码如下所示: import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds) //可选链,设置背景颜色
self.window?.backgroundColor = UIColor.whiteColor() self.window?.rootViewController = ViewController() self.window?.makeKeyAndVisible() //请求用户获取消息推送权限
let setti |