自学 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技术人员提供一个全面的.快捷更 ...
随机推荐
- Eclipse安装git
用Eclipse开发,如果需要团队协作,git作为分布式版本管理工具就是个比较好的选择.下面简单介绍一下git插件的安装方法: 1.Help -- install new software 打开插件安 ...
- SQLServer2008开放windows防火墙配置
为了可以通过TCP/IP协议远程访问SQLServer数据库,需要做以下几点: 在SQLServer所运行的服务器上,我们必须找到SQLServer所侦听的端口然后添加到WIndows防火墙的[允许入 ...
- .Net学习难点讨论系列17 - 线程本地变量的使用
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 谈谈getElementsByClassName()
HTML5中新增的一个方法getElementsByClassName(),但是并非所有浏览器有支持 因此我们构造一个方法兼容这个方法 <script type="text/javas ...
- 性能秒杀log4net的NLogger日志组件(附测试代码与NLogger源码)
NLogger特性: 一:不依赖于第三方插件和支持.net2.0 二:支持多线程高并发 三:读写双缓冲对列 四:自定义日志缓冲大小 五:支持即时触发刷盘机制 六:先按日期再按文件大小滚动Rolling ...
- runtime ---- iOS
1.runtime是什么?runtime是一套底层的C语言的API(包括C语言数据类型,C语言函数) 实际上平时我们写的OC代码底层都是基于runtime,实际上也就是最后都转成了runtime代码 ...
- stringBuffer的使用及字符串比较的区别
/* * 关于equals()和==: 对于String简单来说就是比较两字符串的Unicode序列是否相当,如果相等返回true; * 而==是比较两字符串的地址是否相同,也就是是否是同一个字符串的 ...
- CountDownLatch类的使用
java.util.concurrent.CountDownLatch是一个并发构造,它允许多个线程等候特定的操作完成. CountDownLatch用一个数字初始化,通过调用countDown()方 ...
- 读书笔记 effective c++ Item 16 成对使用new和delete时要用相同的形式
1. 一个错误释放内存的例子 下面的场景会有什么错? std::]; ... delete stringArray 一切看上去都是有序的.new匹配了一个delete.但有一些地方确实是错了.程序的行 ...
- 我的Java开发之路
拉拉溜溜学习了半年了.才发现自己现在才进入面向对象.