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
    }
}
复制代码

 

posted @   sims  阅读(1517)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示