最近公司项目不是很忙,偶然间看到编程语言排行榜,看到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. Word常用实用知识1

    Word常用实用知识1 纯手打,可能有错别字,使用的版本是office Word 2013 转载请注明出处,谢谢. 快速输入日期(含格式) [插入]--[日期]   快速输入日期和时间(快捷键) 快速 ...

  2. HDU5475

    An easy problem Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  3. java cooki的使用

    session: 当新客户端发现一个HTTP请求时服务端会创建一个session.并分配一个sessionID作为服务端来客户端的识别,session对象会 保存在服务端.此时session对象处天N ...

  4. SysLog简介和java操作实例

    什么是SysLog syslog协议属于一种主从式协议:syslog发送端会传送出一个小的文字讯息(小于1024字节)到syslog接收端.接收端通常名为“syslogd”.“syslog daemo ...

  5. .Net程序员学用Oracle系列(16):访问数据库(ODP.NET)

    1..Net for Oracle 常见数据库驱动 1.1.微软提供的驱动 1.2.甲骨文提供的驱动 1.3.其它厂商提供的驱动 2.ODP.NET 常见问题分析 2.1.参数化问题 2.2.方法调用 ...

  6. ZooKeeper 学习笔记

    ZooKeeper学习笔记 1.   zookeeper基本概念 zookeeper是一个分布式的,开放源码的分布式应用程序协调服务,是hadoop和Habase的重要组件,是为分布式应用提供一致性服 ...

  7. centos 安装gcc->联网 问题解决

    本篇部分摘抄至TD_时缔 VMware虚拟机下安装centosmini版本,安装后第一件事就是yum update 但是有错:cannot find a valid baseurl for repo ...

  8. angular.js学习笔记:实现商品价格计算实例

    <!DOCTYPE html> <html lang="en" ng-app> <!-- ng-app:初始化的指令 也可以解析局部--> &l ...

  9. 学习笔记——Java数字处理类

    1.数字格式化 使用Java.text.DecimalFormat格式化数字,一般使用其中的DecimalFormat类.如: import java.text.DecimalFormat; publ ...

  10. Asp.net mvc 4.0 高级编程 百度云下载

    Asp.net mvc 4.0 高级编程 百度云下载地址:链接:http://pan.baidu.com/s/1o6zFvOe 密码:xyss 1.基于 ASP.NET MVC4.0 + WebAPI ...