zoukankan      html  css  js  c++  java
  • 自学 iOS

    最近公司项目不是很忙,偶然间看到编程语言排行榜,看到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 / 255, green: green / 255, blue: blue / 255, alpha: 1);
    }
    
    var RGBAColor: (CGFloat, CGFloat, CGFloat, CGFloat) -> UIColor = {red, green, blue, alpha in
        return UIColor(red: red / 255, green: green / 255, blue: blue / 255, 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(8, 1, 34)
            bottomLeft.backgroundColor = RGBColor(82, 91, 252)
            bottomRight.backgroundColor = RGBColor(102, 189, 9)
            
            topBox.snp.makeConstraints { (make) in
                make.width.equalTo(SCREEN_WIDTH)
                make.height.equalTo(SCREEN_HEIGHT * 0.4)
                make.left.equalTo(self.view).offset(0)
                make.top.equalTo(self.view).offset(0)
            }
            
            resertBtn.setTitle("Reset", for: UIControlState.normal)
            resertBtn.setTitleColor(RGBColor(255, 255, 255), 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: 100)
            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(100)
            }
            
            resertBtn.snp.makeConstraints { (make) in
                make.width.equalTo(120)
                make.top.equalTo(self.topBox).offset(20)
                make.height.equalTo(20)
                make.right.equalTo(self.topBox.snp.right).offset(-20)
            }
            
            bottomLeft.snp.makeConstraints { (make) in
                make.width.equalTo(SCREEN_WIDTH * 0.5)
                make.top.equalTo(topBox.snp.bottom).offset(0)
                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(0)
                make.top.equalTo(bottomLeft).offset(0)
            }
            
            bottomRight.snp.makeConstraints { (make) in
                make.left.equalTo(bottomLeft.snp.right).offset(0)
                make.width.equalTo(bottomLeft)
                make.height.equalTo(bottomLeft)
                make.top.equalTo(topBox.snp.bottom).offset(0)
            }
            
            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(0)
                make.top.equalTo(bottomRight).offset(0)
            }
            
            
            
        }
        // 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 在系统设置中展示Version, Build, Git等信息
    iOS10 App跳转到系统设置
    iOS UIPrintInteractionController打印
    iOS U6b3e转字符串
    Xcode8 报 ”xx“is missing from working copy 的问题解决方法
    OC学习篇之---概述 分类: IOS 2014-11-28 19:06 2349人阅读 评论(1) 收藏
    Android中插件开发篇之----类加载器 分类: Android 2014-11-24 12:15 3275人阅读 评论(4) 收藏
    Mac上安装MySql 分类: JavaWeb 2014-11-10 22:11 446人阅读 评论(0) 收藏
    Andrdoid中对应用程序的行为拦截实现方式之----从Java层进行拦截 分类: Android 2014-11-10 11:23 4311人阅读 评论(12) 收藏
    程序猿的克星 2014-11-07 18:11 4519人阅读 评论(2) 收藏
  • 原文地址:https://www.cnblogs.com/qianLL/p/6551499.html
Copyright © 2011-2022 走看看