swift UIView的渐变视图 - Swift 4.1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift UIView的渐变视图 - Swift 4.1相关的知识,希望对你有一定的参考价值。


//
//  GradientView.swift
//
//  Created by abdelrahman mohamed on 3/19/18.
//  Copyright © 2018 abdelrahman mohamed. All rights reserved.
//
import UIKit

@IBDesignable public class GradientView: UIView {
    
    @IBInspectable var firstColor: UIColor!{
        didSet{
            configureGradientLayer()
        }
    }
    @IBInspectable var secondColor: UIColor!{
        didSet{
            configureGradientLayer()
        }
    }
    
    override open class var layerClass: AnyClass {
        return CAGradientLayer.classForCoder()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configureGradientLayer()
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        configureGradientLayer()
    }
    
    
    func configureGradientLayer() {
        let gradientLayer = layer as! CAGradientLayer
        gradientLayer.colors = [secondColor?.cgColor ?? UIColor.deepLilac.cgColor, firstColor?.cgColor ?? UIColor.blueberry.cgColor]
        gradientLayer.startPoint = CGPoint(x: 0.5, y: 0)
        gradientLayer.endPoint = CGPoint(x: 0.5, y: 1)
    }
}

以上是关于swift UIView的渐变视图 - Swift 4.1的主要内容,如果未能解决你的问题,请参考以下文章