iOS - Swift 实现渐变色背景

本文介绍了一个名为ViewTool的Swift类,它提供了设置视图默认渐变背景色、边框,以及垂直和水平渐变的功能,包括设置边框宽度、圆角和颜色。通过实例代码展示了如何为UIView添加自定义的渐变效果。
摘要由CSDN通过智能技术生成

功能: 实现视图添加渐变背景及边框

//
//  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
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值