ngs = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Sound | UIUserNotificationType.Badge,categories: nil) application.registerUserNotificationSettings(settings) return true } } 本案例中,ViewController.swift文件中完整代码如下所示: import UIKit class ViewController: UIViewController { var timeLabel:UILabel! var timeButtons:[UIButton]! var startButton:UIButton! var resetButton:UIButton!
//计算剩余时间
var remainingSeconds:Int = 0 { //属性监视器
willSet { let min = newValue/60 let sec = newValue%60 timeLabel.text = String(NSString(format: "%02d:%02d", min,sec)) } } //计时器计算属性
var timer:NSTimer? var isCounting:Bool = false { //添加属性监视器,当计时开始创建timer否则停止
willSet { if newValue { timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTimer:"), 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: "time is up", message: "", preferredStyle:.Alert) let action = UIAlertAction(title: "Done", 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 ? "Start" : "Stop" startButton.setTitle(title, forState: .Normal) } func setupTimeLabel(){ timeLabel = UILabel() timeLabel.textColor = UIColor.whiteColor() timeLabel.font = UIFont.boldSystemFontOfSize(80) timeLabel.backgroundColor = UIColor.blackColor() timeLabel.textAlignment = NSTextAlignment.Center view.addSubview(timeLabel) } //预设时间按钮的信息
let timeButtonsInfo = [("1min",60),("3min",180),("5min",300),("sec",1)] func setupTimeButtons(){ timeButtons = [] for (title,sec) in timeButtonsInfo { let button = UIButton() button.backgroundColor = UIColor.orangeColor() button.setTitle(title, forState:UIControlState.Normal) button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted) //记录对应的时间给tag
button.tag = sec //给按钮添加点击事件
button.addTarget(self, action: Selector("timeButtonTapped:"), forControlEvents: UIControlEvents.TouchUpInside) view.addSubview(button) timeButtons.append(button) } } func timeButtonTapped (button:UIButton){ remainingSeconds += button.tag } //设置启动,复位按钮
func setupActionButtons() { startButton = UIButton() startButton.backgroundColor = UIColor.redColor() startButton.setTitleColor(UIColor.whiteColor(), forState: .Normal) startButton.setTitleColor(UIColor.blackColor(), forState: .Highlighted) startButton.setTitle("Start", forState: .Normal) view.addSubview(startButton) //添加事件
startButton.addTarget(self, action: "startButtonTapped:", forControlEvents: .TouchUpInside) resetButt |