iOS - Swift 實現漸變色背景


功能: 實現視圖添加漸變背景及邊框

//
//  ViewTool.swift
//  EXOTerra
//
//  Created by huang zhengguo on 2020/10/16.
//  Copyright © 2020 huang zhengguo. All rights reserved.
//

import Foundation

// 視圖工具類
class ViewTool {
    /**
     * 設置默認漸變色邊框
     *
     * @param view 要設置的視圖
     *
     */
    static func setDefaultBorder(view: UIView) -> Void {
        self.setDefaultBorder(view: view, borderWidth: 2.0)
    }
    
    /**
     * 設置漸變色邊框
     *
     * @param view 要設置的視圖
     * @param borderWidth 邊框寬度
     *
     */
    static func setDefaultBorder(view: UIView, borderWidth: CGFloat) -> Void {
        let cornerRadius = view.layer.cornerRadius
        let viewHeight = view.frame.size.height
        let viewWidth = view.frame.size.width
        
        // 繪制左邊框
        self.setGradientBackgroundColor(view: view, frame: CGRect.init(x: 0, y: cornerRadius, width: borderWidth, height: viewHeight - 2 * cornerRadius), colors: [GlobalConstant.GRADIENT_START_COLOR, GlobalConstant.GRADIENT_START_COLOR], horizontal: true)
        
        // 繪制右邊框
        self.setGradientBackgroundColor(view: view, frame: CGRect.init(x: viewWidth - borderWidth, y: cornerRadius, width: borderWidth, height: viewHeight - 2 * cornerRadius), colors: [GlobalConstant.GRADIENT_END_COLOR, GlobalConstant.GRADIENT_END_COLOR], horizontal: true)
        
        // 繪制上邊框
        self.setGradientBackgroundColor(view: view, frame: CGRect.init(x: cornerRadius, y: 0.0, width: viewWidth - 2 * cornerRadius, height: borderWidth), colors: [GlobalConstant.GRADIENT_START_COLOR, GlobalConstant.GRADIENT_END_COLOR], horizontal: true)
        
        // 繪制底邊框
        self.setGradientBackgroundColor(view: view, frame: CGRect.init(x: cornerRadius, y: viewHeight - borderWidth, width: viewWidth - 2 * cornerRadius, height: borderWidth), colors: [GlobalConstant.GRADIENT_START_COLOR, GlobalConstant.GRADIENT_END_COLOR], horizontal: true)
    }
    
    /*
     * 設置視圖垂直漸變背景色
     *
     * @param view 要設置的視圖
     * @param frame 區域大小
     * @param cornerRadius 圓角
     *
     */
    static func setDefaultVerticalGradientBackgroundColor(view: UIView, frame: CGRect, cornerRadius: CGFloat = 0.0) -> Void {
        setGradientBackgroundColor(view: view, frame: frame, colors: [GlobalConstant.GRADIENT_START_COLOR, GlobalConstant.GRADIENT_END_COLOR], horizontal: false, cornerRadius: cornerRadius)
    }
    
    /*
     * 設置視圖漸變背景色
     *
     * @param view 要設置的視圖
     * @param frame 區域大小
     * @param cornerRadius 圓角
     *
     */
    static func setDefaultHorizontalGradientBackgroundColor(view: UIView, frame: CGRect, cornerRadius: CGFloat = 0.0) -> Void {
        setGradientBackgroundColor(view: view, frame: frame, colors: [GlobalConstant.GRADIENT_START_COLOR, GlobalConstant.GRADIENT_END_COLOR], horizontal: true, cornerRadius: cornerRadius)
    }
    
    /*
     * 移除視圖漸變背景色
     *
     * @param view 要設置的視圖
     *
     */
    static func removeGradientColorBackground(view: UIView) -> Void {
        if view.layer.sublayers == nil {
            return;
        }
        
        var layersToRemove: [CAGradientLayer] = [CAGradientLayer]()
        for layer in view.layer.sublayers! {
            if layer.isKind(of: CAGradientLayer.self) {
                layersToRemove.append(layer as! CAGradientLayer)
            }
        }
        
        for layer in layersToRemove {
            layer.removeFromSuperlayer()
        }
    }
    
    /*
     * 設置視圖漸變背景色
     *
     * @param view 要設置的視圖
     * @param frame 區域大小
     * @param colors 漸變顏色數組
     * @param horizontal 漸變方向
     * @param cornerRadius 圓角大小
     *
     */
    static func setGradientBackgroundColor(view: UIView, frame: CGRect, colors: [CGColor], horizontal: Bool, cornerRadius: CGFloat = 0.0) -> Void {
        let startPoint = CGPoint.init(x: 0.0, y: 0.0)
        var endPoint = CGPoint.init(x: 1.0, y: 0.0)
        if horizontal == false {
            endPoint = CGPoint.init(x: 0.0, y: 1.0)
        }
        
        let gradientLayer: CAGradientLayer = getGradientLayer(frame: frame, startPoint: startPoint, endPoint: endPoint, locations: [ 0.0, 1.0], colors: colors)
        
        gradientLayer.zPosition = -10000
        gradientLayer.cornerRadius = cornerRadius
        
        view.layer.addSublayer(gradientLayer)
    }
    
    /**
     *
     * 獲取一個顏色漸變層
     *
     * @param frame 大小
     * @param startPoint 顏色漸變起點
     * @param endPoint 顏色漸變終點
     * @param locations 顏色數組對應的點
     * @param colors 漸變顏色數組
     *
     * @return 顏色漸變層
     *
     */
    static func getGradientLayer(frame: CGRect, startPoint: CGPoint, endPoint: CGPoint, locations: [NSNumber], colors: [CGColor]) -> CAGradientLayer {
        let gradientLayer = CAGradientLayer.init()
        
        gradientLayer.frame = frame
        gradientLayer.startPoint = startPoint
        gradientLayer.endPoint = endPoint
        gradientLayer.locations = locations
        gradientLayer.colors = colors
        
        return gradientLayer
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM