最近公司项目不是很忙,偶然间看到编程语言排行榜,看到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 项目 第一天的更多相关文章

  1. 自学 iOS – 三十天三十个 Swift 项目

    自学 iOS – 三十天三十个 Swift 项目 github源码地址:https://github.com/allenwong/30DaysofSwift

  2. 程序员编程艺术第三十六~三十七章、搜索智能提示suggestion,附近点搜索

    第三十六~三十七章.搜索智能提示suggestion,附近地点搜索 作者:July.致谢:caopengcs.胡果果.时间:二零一三年九月七日. 题记 写博的近三年,整理了太多太多的笔试面试题,如微软 ...

  3. 自学 iOS - 三十天三十个 Swift 项目 第三天

    做了这个小demo 之后  感觉OC 和swift 还是有很大的差别的 自己还是要去多看些swift的语法 用的不是很熟练 1.这个demo 的资源文件 我都是用原工程的 2.同样的自定义cell 的 ...

  4. 自学 iOS - 三十天三十个 Swift 项目 第二天

    继续做仿造着别人的第二个 1.首先下载 一些字体 网上搜索 "造字工房" 2.把下载的相应字体文件放到工程之中,就Ok了 不多说 效果如下 可以下面这个方法 检索项目里面所有的字体 ...

  5. Unity 游戏框架搭建 2019 (三十、三十一) MenuItem 显示顺序问题 & 类的提取

    在上一篇,我们得出了两个核心的学习思路: 根据问题去学习,并收集. 主动学习,并思考适用场景. 我们今天解决 MenuItem 显示顺序问题. 目前 MenuItem 显示如图所示: 我们来看下 Me ...

  6. Unity 游戏框架搭建 2019 (三十六~三十八) partial与public

    在上一篇,我们把菜单的顺序从头到尾整理了一遍.在整理菜单顺序的过程中,记录了一个要做的事情. 要做的事情: (完成) 备份:导出文件,并取一个合理的名字. 整理完菜单顺序后,学习新的知识,解决随着示例 ...

  7. 【Android Studio安装部署系列】三十四、将Eclipse项目导入到Android Studio中

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 我采用的是笨方法:新创建Android Studio项目,然后将Eclipse项目中的目录一一复制到Android Studio项目 ...

  8. iOS开发之Todo List for Swift项目

    一直从事Windows Phone开发,但对iOS开发一直有所好奇,于是在MBP到手之际,顺手安装了Xcode.移动互联网开发的相似性,使得我能快速地了解和认识了iOS的开发框架体系,在看完了Appl ...

  9. swift项目第一天:环境部署

    一:项目部署 项目部署 一.开源中国(OSChina) 网站地址:https://git.oschina.net/ 开源中国社区成立于2008年8月,其目的是为中国的IT技术人员提供一个全面的.快捷更 ...

随机推荐

  1. Dockerfile的书写规则及指令使用方法

    Dockfile是一种被Docker程序解释的脚本,Dockerfile由一条一条的指令组成,每条指令对应Linux下面的一条命令.Docker程序将读取Dockerfile,根据指令生成定制的ima ...

  2. 爱你不容易 —— Stream详解

    作为前端,我们常常会和 Stream 有着频繁的接触.比如使用 gulp 对项目进行构建的时候,我们会使用 gulp.src 接口将匹配到的文件转为 stream(流)的形式,再通过 .pipe() ...

  3. ArcGIS制图技巧系列(1)还原真实的植被

    ArcGIS制图技巧系列(1)还原真实的植被 by 李远祥 在GIS数据中,植被一般都是面装要素的形式存在.很多人在使用植被渲染的时候,一般会采用填充符号去渲染.而在ArcGIS中,填充符号要么就是纯 ...

  4. runloop和runtime

    runloop Runloop是事件接收和分发机制的一个实现. 一个程序从main函数开始,函数执行完毕之后就会退出,iOS程序也是一样的,但是我们从没看到过iOS程序打开之后直接闪退,肯定是有一些东 ...

  5. jQuery的拾色器

    代码如下 1.js <link href="css/farbtastic.css" rel="stylesheet" /> <script t ...

  6. PCB信号集

    每一个进程都有一个pcb进程控制块,用来控制进程的信息,同时信号在pcb中有两个队列去维护他,一个是未决信号集,每一位对应一个信号的状态,0,1,1表示未决态,另一个是信号屏蔽字(阻塞信号集),也就0 ...

  7. [故障公告]博客站点遭遇超过20G的流量攻击被阿里云屏蔽

    2017年2月21日17:34,突然收到阿里云的通知: 您的IP受到攻击流量已超过云盾DDoS基础防护的带宽峰值,服务器的所有访问已被屏蔽,如果35分钟后攻击停止将自动解除否则会延期解除... 紧接着 ...

  8. 每天一个linux命令(56)--crontab命令

    上一节学习了 at  命令是针对仅运行一次的任务,循环运行的例行性计划任务,Linux 系统则是由 cron(crond)这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个 ...

  9. Node.js~在linux上的部署~pm2管理工具的使用

    之前写了两篇关于在linux上部署nodejs的文章,大家如果没有基础可以先看前两篇<Node.js~在linux上的部署>,<Node.js~在linux上的部署~外网不能访问no ...

  10. 获取页面中任意一个元素距离body的偏移量

    //offSet:等同于jQuery中的offSet方法,获取页面中任意一个元素距离body的偏移量function offSet(curEle) { var totalLeft = null; va ...