设为首页 加入收藏

TOP

Swift-----类型转换 、 嵌套类型 、 扩展 、 协议 、 访问控制(六)
2017-10-10 12:17:31 】 浏览:2038
Tags:Swift----- 类型 转换 扩展 协议 访问 控制
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
首页 上一页 3 4 5 6 7 下一页 尾页 6/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇CocoaPods的安装和使用那些事(Xc.. 下一篇Swift语法笔记

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目