Swift3之UIView

網絡上Swift的基礎知識很多,但是對於UI的資料就不太全面了
這裏提供一篇UIView的常用功能介紹,適合iOS初學者,也適合OC人員轉Swift,廢話不多書,直接看代碼吧!
github上有UIKitDemo,可以下載學習, 點擊進入Github

//
//  DMViewViewController.swift
//  UIKitDemo
//
//  Created by apple on 2017/6/28.
//  Copyright © 2017年 JIN. All rights reserved.
//

import UIKit

class DMViewViewController: DMRootViewController {

    //swift中變量必須在類init前初始化
    let redView = UIView()
    let cyanView = UIView()
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        redViewSetting()//1⃣️.先進入函數內學習 常用的屬性 設置
        redViewLayerSetting()//2⃣️.進入方法內學習 layer相關的設置
        cyanViewSetting()

        self.view.addSubview(redView)
        self.view.addSubview(cyanView)
        viewMove()// 3⃣️.學習下面的圖層相關
     }

    //1⃣️ UIView的常用屬性設置
    func redViewSetting() {
        //swift的代碼提示:在輸入CGRect之後輸入( 會自動提示初始化方法
        //* view的位置設置(有frame,bounds,center三個屬性)
        redView.frame = CGRect(x: 100, y: 100, width: 100, height: 100)

        //* view的背景色設置
        redView.backgroundColor = UIColor.red

        //* 設置Tag值,tag值就是對view編輯一個數字,通過數字能夠取到view,或區分view
        redView.tag = 1000
        //Tag值取view
//        self.view.viewWithTag(1000)

        //* 是否支持用戶手勢,默認爲flase
        redView.isUserInteractionEnabled = true

        //* 手勢
        let tapRedView = UITapGestureRecognizer(target: self, action: #selector(tapRedViewAction))
        redView.addGestureRecognizer(tapRedView)

    }

    //2⃣️ CALayer層的設置
    func redViewLayerSetting(){
        //* 設置圓角 設置圓角一般配合masksToBounds使用,當你設置的圓角沒有出現時,試試加上masksToBounds的設置
        redView.layer.cornerRadius = 10
        //        redView.layer.masksToBounds = true
        //        redView.clipsToBounds = true

        //* 陰影
        redView.layer.shadowColor = UIColor.blue.cgColor// 陰影的顏色
        redView.layer.shadowOpacity = 0.7// 陰影透明度 必須設置,因爲默認是0 即全透明
        redView.layer.shadowOffset = CGSize(width: 5, height: 5)//陰影相對於view的偏移量
        redView.layer.shadowRadius = 10// 陰影擴散的範圍

        //* 邊框
        redView.layer.borderColor = UIColor.green.cgColor
        redView.layer.borderWidth = 2
    }

    //手勢的點擊方法
    func tapRedViewAction(sender: UITapGestureRecognizer){
        let tempView = sender.view
        //* UIView的動畫
        UIView.animate(withDuration: 0.5, animations: {
            //仿射變換,用於在二維空間做旋轉,縮放和平移
            tempView?.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
        }) { complete in
            UIView.animate(withDuration: 0.5, animations: { 
                tempView?.transform = CGAffineTransform(scaleX: 1, y: 1)
            })
        }
        tempView?.backgroundColor = UIColor(red:rand(), green: rand(), blue: rand(), alpha: 1.0)
    }

    //獲取隨機數
    func rand() -> CGFloat{
        return CGFloat(arc4random()%255)/255.0
    }

    func  cyanViewSetting()  {
        cyanView.backgroundColor = UIColor.cyan
        cyanView.bounds = CGRect(x: 0, y: 0, width: 200, height: 200)//view的邊界大小,即寬和高的設置
        cyanView.center = redView.center//view的中心點位置設置

    }

    //3⃣️ 視圖的圖層層級
    func viewMove()
    {
        //view的顯示順序和添加順序有關,後添加的圖層在先添加的上面,此時cyanView是壓在redView上面的
        //        self.view.bringSubview(toFront: redView)//把redView圖層移到最前面
        //        self.view.insertSubview(cyanView, belowSubview: redView)//把cyanView插入到redView下面
        self.view.sendSubview(toBack: cyanView)//把cyanView放到最後

        //        cyanView.removeFromSuperview()//cyanView從父view上移除

        //        var subViews = self.view.subviews//獲取view到所有子試圖(添加到view上的視圖爲子試圖)

        //        cyanView.superview //cyanView所添加到的視圖,也就是父視圖

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章