自学 iOS - 三十天三十个 Swift 项目 第一天
最近公司项目不是很忙,偶然间看到编程语言排行榜,看到swift 已经排到前10了,然OC排名也越来越后了,感觉要上车了,虽然现在项目都是用OC写的,但是swift是一种趋势。在网上看到“自学 iOS - 三十天三十个 Swift 项目” 这篇博客,我也想自己在闲暇之余学习下swift,在看了2天的swift 的语法过后,才开始做这个,语法看的也不是很懂,有些部分。还是要自己动手
废话不多说
先上效果

这是这个简单的效果
1.首先 我去网上找了一下 swift自动布局的框架 “SnapKit” 用起来和Massory 差不多 上手很快
然后 对swift的一些必要东西 进行了宏定义 类似于OC 的PCH文件 swift 里面就比较简单 新建立个swift文件就可以了
代码如下
import UIKit import SnapKit let SCREEN_WIDTH = UIScreen.main.bounds.size.width
let SCREEN_HEIGHT = UIScreen.main.bounds.size.height var RGBColor: (CGFloat, CGFloat, CGFloat) -> UIColor = {red, green, blue in
return UIColor(red: red / , green: green / , blue: blue / , alpha: );
} var RGBAColor: (CGFloat, CGFloat, CGFloat, CGFloat) -> UIColor = {red, green, blue, alpha in
return UIColor(red: red / , green: green / , blue: blue / , alpha: alpha);
}
然后主控制器里面的代码(由于代码比较简单 我就没写注释了 )
import UIKit
class ViewController: UIViewController {
lazy var topBox = UIView()
lazy var bottomLeft = UIView()
lazy var bottomRight = UIView()
lazy var resertBtn = UIButton()
lazy var startBtn = UIButton()
lazy var pauseBtn = UIButton()
lazy var numberLabel = UILabel()
var timer: Timer!
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(topBox)
self.view.addSubview(bottomLeft)
self.view.addSubview(bottomRight)
topBox.backgroundColor = RGBColor(, , )
bottomLeft.backgroundColor = RGBColor(, , )
bottomRight.backgroundColor = RGBColor(, , )
topBox.snp.makeConstraints { (make) in
make.width.equalTo(SCREEN_WIDTH)
make.height.equalTo(SCREEN_HEIGHT * 0.4)
make.left.equalTo(self.view).offset()
make.top.equalTo(self.view).offset()
}
resertBtn.setTitle("Reset", for: UIControlState.normal)
resertBtn.setTitleColor(RGBColor(, , ), for: UIControlState.normal)
// resertBtn.backgroundColor = UIColor.red
resertBtn.addTarget(self, action: #selector(resert) , for: UIControlEvents.touchUpInside)
self.topBox.addSubview(resertBtn)
numberLabel.text = "0.0"
numberLabel.font = UIFont.boldSystemFont(ofSize: )
numberLabel.textColor = UIColor.white
numberLabel.textAlignment = .center
topBox.addSubview(numberLabel)
numberLabel.snp.makeConstraints { (make) in
make.center.equalTo(topBox)
make.width.equalTo(topBox)
make.height.equalTo()
}
resertBtn.snp.makeConstraints { (make) in
make.width.equalTo()
make.top.equalTo(self.topBox).offset()
make.height.equalTo()
make.right.equalTo(self.topBox.snp.right).offset(-)
}
bottomLeft.snp.makeConstraints { (make) in
make.width.equalTo(SCREEN_WIDTH * 0.5)
make.top.equalTo(topBox.snp.bottom).offset()
make.left.equalTo(self.view)
make.bottom.equalTo(self.view)
}
startBtn.setTitle("开始", for: .normal)
startBtn.setTitleColor(UIColor.white, for: .normal)
startBtn.addTarget(self, action: #selector(start), for: .touchUpInside)
bottomLeft.addSubview(startBtn)
startBtn.snp.makeConstraints { (make) in
make.width.equalTo(bottomLeft)
make.height.equalTo(bottomLeft)
make.left.equalTo(bottomLeft).offset()
make.top.equalTo(bottomLeft).offset()
}
bottomRight.snp.makeConstraints { (make) in
make.left.equalTo(bottomLeft.snp.right).offset()
make.width.equalTo(bottomLeft)
make.height.equalTo(bottomLeft)
make.top.equalTo(topBox.snp.bottom).offset()
}
pauseBtn.setTitle("停止", for: .normal)
pauseBtn.setTitleColor(UIColor.white, for: .normal)
pauseBtn.addTarget(self, action: #selector(pause), for: .touchUpInside)
bottomRight.addSubview(pauseBtn)
pauseBtn.snp.makeConstraints { (make) in
make.width.equalTo(bottomRight)
make.height.equalTo(bottomRight)
make.left.equalTo(bottomRight).offset()
make.top.equalTo(bottomRight).offset()
}
}
// MARK:清零的点击事件
func resert() {
startBtn.isUserInteractionEnabled = true
pauseBtn.isUserInteractionEnabled = true
numberLabel.text = "0.0"
timer.invalidate()
}
// MARK:开始事件
func start(){
startBtn.isUserInteractionEnabled = false
pauseBtn.isUserInteractionEnabled = true
// 创建并启动定时器
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(numberChange), userInfo: self, repeats: true)
timer.fire()
}
func numberChange() {
let number = NSString(string: numberLabel.text!).doubleValue
let changeNumber = number + 0.1
numberLabel.text = "\(changeNumber)"
}
// MARK:暂停
func pause() {
pauseBtn.isUserInteractionEnabled = false
startBtn.isUserInteractionEnabled = true
timer.invalidate()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
自学 iOS - 三十天三十个 Swift 项目 第一天的更多相关文章
- 自学 iOS – 三十天三十个 Swift 项目
自学 iOS – 三十天三十个 Swift 项目 github源码地址:https://github.com/allenwong/30DaysofSwift
- 程序员编程艺术第三十六~三十七章、搜索智能提示suggestion,附近点搜索
第三十六~三十七章.搜索智能提示suggestion,附近地点搜索 作者:July.致谢:caopengcs.胡果果.时间:二零一三年九月七日. 题记 写博的近三年,整理了太多太多的笔试面试题,如微软 ...
- 自学 iOS - 三十天三十个 Swift 项目 第三天
做了这个小demo 之后 感觉OC 和swift 还是有很大的差别的 自己还是要去多看些swift的语法 用的不是很熟练 1.这个demo 的资源文件 我都是用原工程的 2.同样的自定义cell 的 ...
- 自学 iOS - 三十天三十个 Swift 项目 第二天
继续做仿造着别人的第二个 1.首先下载 一些字体 网上搜索 "造字工房" 2.把下载的相应字体文件放到工程之中,就Ok了 不多说 效果如下 可以下面这个方法 检索项目里面所有的字体 ...
- Unity 游戏框架搭建 2019 (三十、三十一) MenuItem 显示顺序问题 & 类的提取
在上一篇,我们得出了两个核心的学习思路: 根据问题去学习,并收集. 主动学习,并思考适用场景. 我们今天解决 MenuItem 显示顺序问题. 目前 MenuItem 显示如图所示: 我们来看下 Me ...
- Unity 游戏框架搭建 2019 (三十六~三十八) partial与public
在上一篇,我们把菜单的顺序从头到尾整理了一遍.在整理菜单顺序的过程中,记录了一个要做的事情. 要做的事情: (完成) 备份:导出文件,并取一个合理的名字. 整理完菜单顺序后,学习新的知识,解决随着示例 ...
- 【Android Studio安装部署系列】三十四、将Eclipse项目导入到Android Studio中
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 我采用的是笨方法:新创建Android Studio项目,然后将Eclipse项目中的目录一一复制到Android Studio项目 ...
- iOS开发之Todo List for Swift项目
一直从事Windows Phone开发,但对iOS开发一直有所好奇,于是在MBP到手之际,顺手安装了Xcode.移动互联网开发的相似性,使得我能快速地了解和认识了iOS的开发框架体系,在看完了Appl ...
- swift项目第一天:环境部署
一:项目部署 项目部署 一.开源中国(OSChina) 网站地址:https://git.oschina.net/ 开源中国社区成立于2008年8月,其目的是为中国的IT技术人员提供一个全面的.快捷更 ...
随机推荐
- 大咖,我能转行做UX设计师吗?
前几天,有个朋友找到我,叫我给分析下他适不适合转UX设计.他的专业是建筑设计,之所以要辞职,也就是公司破事多,老板又不看重他.看到UX设计这个行业的前景很不错,想要转行.他说的也没错, 现在的UX设计 ...
- HTML5常用标签分类
1.行级元素标签:a.span.sup.sub.em.b.big.i.strong 2.块元素标签:div.p.h1~h6.ul.ol.li.table.form.article.footer.hea ...
- 《JAVASCRIPT高级程序设计》JSON语法/解析/序列化
JSON是一种数据格式,不是一种编程语言. 一.语法 JSON语法可以表示以下三种类型的值:简单值.对象.数组. 1.简单值 最简单的JSON数据值就是简单值: 5 "hello world ...
- Codeforce 712A Memory and Crow
A. Memory and Crow time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- Linux select I/O 复用
用途 在处理多个socket套接字的时候,会很自然的遇到一个问题:某个套接字什么时候可读?什么时候可写?哪些套接字是需要关闭的?我们可以回忆一下,一般我们在最开始编写socket程序的时候,send, ...
- UITableView、UICollectionView行高/尺寸自适应
UITableView 我们都知道UITableView从iOS 8开始实现行高的自适应相对比较简单,首先必须设置estimatedRowHeight给出预估高度,设置rowHeight为UITabl ...
- Jasmine 的自定义部分
自定义toEqual toEqual mathers 支持用户自定义比较方法,使用的是jasmine.addCustomEquallyTester(myCustomEqualltyFunction)方 ...
- 初识Jenkins
近期,接手了一个活,我要搭一个Jenkins持续集成的平台,所以,就把这次工作的收获分享给大家了. Jenkins是什么 Jenkins插件配置 Jenkins怎么用 新建job 系统配置 添加用户 ...
- windows下搭建Nexus3私服和基于IDEA15的Maven学习笔记
搭建Nexus私服. 首先去官网下载window下用的zip文件.https://www.sonatype.com/download-oss-sonatype. 解压之后包含下面两个文件 进入nexu ...
- 【原创】python中文编码问题深入分析(二):print打印中文异常及显示乱码问题分析与解决
在学习python以及在使用python进行项目开发的过程中,经常会使用print语句打印一些调试信息,这些调试信息中往往会包含中文,如果你使用python版本是python2.7,或许你也会遇到和我 ...