设为首页 加入收藏

TOP

Swift-----类型转换 、 嵌套类型 、 扩展 、 协议 、 访问控制(四)
2017-10-10 12:17:31 】 浏览:2057
Tags:Swift----- 类型 转换 扩展 协议 访问 控制
添加新的实例方法和类型方法。
3.3 步骤 实现此案例需要按照如下步骤进行。 步骤一:创建window对象和根视图控制器 首先在AppDeleagte类中程序加载完成的方法中创建一个和屏幕大小一样的window对象,并将window的背景颜色设置为白色,代码如下所示: 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?.makeKeyAndVisible() return true } 然后创建window的根视图控制器,根视图控制器是ViewController类型,Swift项目中不需要导入头文件就可以使用项目中定义好的ViewController类,代码如下所示: 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() return true } 由上面的代码可以看出OC语言提供的类和方法完全可以在Swift中使用,只是换成Swift的语法而已,IOS程序的运行原理和开发思想与之前所学完全一致。 步骤二:搭建界面 计时器的界面上方是一个现实倒计时的Label控件,下方是一排预设的时间按钮,最下方是启动和复位按钮,首先将这些控件全都设置为ViewController的存储属性,在viewDidLoad方法中进行初始状态的设置,布局代码写在viewDidLayoutSubviews方法中,代码如下所示: class ViewController: UIViewController { var timeLabel:UILabel! var timeButtons:[UIButton]! var startButton:UIButton! var resetButton:UIButton! 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 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) resetButton = 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) } 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
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 4/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇CocoaPods的安装和使用那些事(Xc.. 下一篇Swift语法笔记

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目