on = UIButton() resetButton.backgroundColor = UIColor.redColor() resetButton.setTitleColor(UIColor.whiteColor(), forState: .Normal) resetButton.setTitleColor(UIColor.blackColor(), forState: .Highlighted) resetButton.setTitle("Reset", forState: .Normal) view.addSubview(resetButton) resetButton.addTarget(self, action: "resetButtonTapped:", forControlEvents: .TouchUpInside) } func startButtonTapped(button:UIButton){ isCounting = !isCounting if isCounting { //添加消息推送
createAndFireLocalNotificationAfterSeconds(Double(remainingSeconds)) }else { //计时结束退出消息推送
UIApplication.sharedApplication().cancelAllLocalNotifications() } } func resetButtonTapped(button:UIButton){ remainingSeconds = 0 } //添加消息推送方法
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) } override func viewDidLoad() { super.viewDidLoad() setupTimeLabel() setupTimeButtons() setupActionButtons() } //布局代码
override func viewDidLayoutSubviews() { //时间窗口的布局
timeLabel.frame = CGRect(x: 10, y: 40, width: view.bounds.size.width-20, height: 120) //时间按钮的布局:按钮大小:64x44,按钮之间的间隔
let cnt = timeButtons.count - 1 let width = view.bounds.width - 10*2.0 - CGFloat(timeButtons.count) * 64.0 let gap = width / CGFloat(cnt) for (index, button) in enumerate(timeButtons) { let buttonLeft = 10.0 + CGFloat(index) * (64.0 + gap) button.frame = CGRectMake(CGFloat(buttonLeft), view.bounds.height-120.0, 64, 44) } //启动复位按钮的布局
startButton.frame = CGRectMake(10, view.bounds.height - 60, view.bounds.width - 20 - 100, 44) resetButton.frame = CGRectMake(10 + startButton.frame.width+20, view.bounds.height - 60, 80, 44) } }
|