Swift

iOS 自定义圆角 + 阴影

2020-12-20  本文已影响0人  朽木自雕也

背景

在 APP 页面开发过程中,设计师会要求UI控件圆润一些,再带点阴影,圆角的样式也是各种各样,有的UI左边圆角,有的UI右边圆角,有的要求UI的每个角圆角大小不同。现如今打开 App Store 看看,可以发现 today、游戏、App 几个地方都使用了卡片式布局,个人觉得确实比以前好看了许多,我们公司的 UI 也紧跟时代美学前沿,大部分页面都使用卡片式布局来展示,于是我也对 iOS 原生开发圆角和阴影做一个了解,对实现高度灵活、高性能的圆角阴影做了一些思考与实践。

思考

圆角的绘制方法

  1. 使用 CAlayer 的 cornerRadius 属性设置圆角
  2. 设置 CAlayer 的 mask 属性
  3. 通过 layerClass 返回 CAShapeLayer,指定当前 UIView 的根 layer 类型,通过设置 CAShapeLayer 的 path 来实现圆角绘制

阴影的绘制方法

  1. 叫 UI设计师切一张阴影背景图,我看安卓哥们就经常让UI切阴影
  2. 不指定 shadowPath绘制阴影,会造成离屏渲染
  3. 指定 shadowPath 绘制阴影,不会造成离屏渲染,在view使用自动布局的情况下,不好指定 shadowPath,可以通过重写 UIView 的 layoutSubviews 方法动态指定 shadowPath 的路径,实现过程相对麻烦。

圆角与阴影共存

  1. UIView 的 clipsToBounds 属性设置为 true,会把超出视图范围外的部分裁剪掉不显示,若要使 圆角和阴影共存,那么 clipsToBounds 必须设置为 false,庆幸的是UIView 的 clipsToBounds属性值默认为false
  2. 使用layer的mask来给UIView 切圆角会把超出mask范围外的部分裁剪掉,若要使圆角与阴影共存,此方法不可取。
  3. 指定UIView的根 layer 为 CAShapeLayer 类型,通过设置 layer.path 实现圆角,这时的path是CGPath类型,CGPath 非常灵活,fillColor 当做背景填充色,strokeColor 从当边框颜色,使用 layer.path 作为 shadowPath 一举两得。理论上是行得通的。
  4. 让设计师切一张带阴影的背景图,这个方法简单粗暴,如果好意思在设计师小姐姐面前说这个效果我做不出来,给我切张带阴影的背景图好不,这样也行。

自定义 UIView 实现圆角阴影

  1. 自定义 MTCircularView,使得当前的视图的根Layer 为 CAShapeLayer 类型
/** 圆角视图 */
class MTCircularView: UIView {
    var radiusLayer:CAShapeLayer {
        return self.layer as! CAShapeLayer
    }
    override class var layerClass: AnyClass {
        return CAShapeLayer.self
    }
}
  1. 定义一个 MTCirculars 结构体来描述圆角,控件有四个角,分别用四个变量来表示各个角的圆角大小
/** 圆角大小 */
struct MTCirculars {
    var topLeft:CGFloat = 0
    var topRight:CGFloat = 0
    var bottomLeft:CGFloat = 0
    var bottomRight:CGFloat = 0
    
    public static let zero = MTCirculars(topLeft: 0, topRight: 0, bottomLeft: 0, bottomRight: 0)
    public init(topLeft: CGFloat, topRight:CGFloat, bottomLeft:CGFloat, bottomRight:CGFloat) {
        self.topLeft = topLeft
        self.topRight = topRight
        self.bottomLeft = bottomLeft
        self.bottomRight = bottomRight
    }
    static func ==(v1:MTCirculars, v2:MTCirculars) -> Bool {
        return v1.bottomLeft == v2.bottomLeft
            && v1.bottomRight == v2.bottomRight
            && v1.topLeft == v2.topLeft
            && v1.topRight == v2.topRight
    }
    static func !=(v1:MTCirculars, v2:MTCirculars) -> Bool {
        return !(v1 == v2)
    }
}
  1. 绘制圆角路径,为了代码的可复用性,新建一个 CGMutablePath 的扩展,进行圆角Path的绘制
extension CGMutablePath {
    func addRadiusRectangle(_ circulars: MTCirculars, rect: CGRect) {
        move(to: CGPoint(x: circulars.topLeft, y: rect.minY))
        addLine(to: CGPoint(x: rect.width - circulars.topRight, y: rect.minY))
        addQuadCurve(to: CGPoint(x: rect.width, y: circulars.topRight), control: CGPoint(x: rect.width, y: rect.minY))
        
        addLine(to: CGPoint(x: rect.width, y: rect.height - circulars.bottomRight))
        addQuadCurve(to: CGPoint(x: rect.width - circulars.bottomRight, y: rect.height), control: CGPoint(x: rect.width, y: rect.height))
        
        addLine(to: CGPoint(x: circulars.bottomLeft, y: rect.height))
        addQuadCurve(to: CGPoint(x: rect.minX, y: rect.height - circulars.bottomLeft), control: CGPoint(x: rect.minX, y: rect.height))
        
        addLine(to: CGPoint(x: rect.minX, y: circulars.topLeft))
        addQuadCurve(to: CGPoint(x: circulars.topLeft, y: rect.minY), control: CGPoint(x: rect.minX, y: rect.minY))
        closeSubpath()
    }
}
  1. 设置圆角,在 MTCircularView 中定义一个 MTCirculars 类型的属性,用于描述视图的圆角;重写 layoutSubviews 方法为视图动态设置圆角,此时 MTCircularView 类中的代码如下:
/** 圆角视图 */
class MTCircularView: UIView {
    var radiusLayer:CAShapeLayer {
        return self.layer as! CAShapeLayer
    }
    override class var layerClass: AnyClass {
        return CAShapeLayer.self
    }
    var radiuses:MTCirculars = .zero
    override func layoutSubviews() {
        super.layoutSubviews()
            let path = CGMutablePath()
            path.addRadiusRectangle(radiuses, rect: bounds)
            radiusLayer.path = path
    }
}
  1. 设置阴影 shadowPath,在 layoutSubviews 中设置 使用圆角路径作为 shadowPath,MTCircularView 类的代码如下:
/** 圆角视图 */
class MTCircularView: UIView {
    var radiusLayer:CAShapeLayer {
        return self.layer as! CAShapeLayer
    }
    override class var layerClass: AnyClass {
        return CAShapeLayer.self
    }
    var radiuses:MTCirculars = .zero
    override func layoutSubviews() {
        super.layoutSubviews()
        let path = CGMutablePath()
        path.addRadiusRectangle(radiuses, rect: bounds)
        radiusLayer.path = path
        radiusLayer.shadowPath = path
    }
}
  1. 添加一些便捷方法供客户端调用,代码如下:
/** 圆角视图 */
class MTCircularView: UIView {
    var radius:CGFloat! {
        didSet {
            self.allRadius(radius)
        }
    }
    var radiusLayer:CAShapeLayer {
        return self.layer as! CAShapeLayer
    }
    override class var layerClass: AnyClass {
        return CAShapeLayer.self
    }
    var radiuses:MTCirculars = .zero
    override func layoutSubviews() {
        super.layoutSubviews()
        let path = CGMutablePath()
        path.addRadiusRectangle(radiuses, rect: bounds)
        radiusLayer.path = path
        radiusLayer.shadowPath = path
    }
    /** 顶部圆角 */
    func topRadius(_ size: CGFloat) {
        radiuses = MTCirculars(topLeft: size, topRight: size, bottomLeft: 0, bottomRight: 0)
    }
    /** 底部圆角 */
    func bottomRadius(_ size: CGFloat) {
        radiuses = MTCirculars(topLeft: 0, topRight: 0, bottomLeft: size, bottomRight: size)
    }
    /** 左边圆角 */
    func leftRadius(_ size: CGFloat) {
        radiuses = MTCirculars(topLeft: size, topRight: 0, bottomLeft: size, bottomRight: 0)
    }
    /** 右边圆角 */
    func rightRadius(_ size: CGFloat) {
        radiuses = MTCirculars(topLeft: 0, topRight: size, bottomLeft: 0, bottomRight: size)
    }
    /** 所有圆角 */
    func allRadius(_ size: CGFloat) {
        radiuses = MTCirculars(topLeft: size, topRight: size, bottomLeft: size, bottomRight: size)
    }
}

最后预览一下整体代码

import UIKit

/** 圆角大小 */
struct MTCirculars {
    var topLeft:CGFloat = 0
    var topRight:CGFloat = 0
    var bottomLeft:CGFloat = 0
    var bottomRight:CGFloat = 0
    
    public static let zero = MTCirculars(topLeft: 0, topRight: 0, bottomLeft: 0, bottomRight: 0)
    public init(topLeft: CGFloat, topRight:CGFloat, bottomLeft:CGFloat, bottomRight:CGFloat) {
        self.topLeft = topLeft
        self.topRight = topRight
        self.bottomLeft = bottomLeft
        self.bottomRight = bottomRight
    }
    static func ==(v1:MTCirculars, v2:MTCirculars) -> Bool {
        return v1.bottomLeft == v2.bottomLeft
            && v1.bottomRight == v2.bottomRight
            && v1.topLeft == v2.topLeft
            && v1.topRight == v2.topRight
    }
    static func !=(v1:MTCirculars, v2:MTCirculars) -> Bool {
        return !(v1 == v2)
    }
}

extension CGMutablePath {
    func addRadiusRectangle(_ circulars: MTCirculars, rect: CGRect) {
        move(to: CGPoint(x: circulars.topLeft, y: rect.minY))
        addLine(to: CGPoint(x: rect.width - circulars.topRight, y: rect.minY))
        addQuadCurve(to: CGPoint(x: rect.width, y: circulars.topRight), control: CGPoint(x: rect.width, y: rect.minY))
        
        addLine(to: CGPoint(x: rect.width, y: rect.height - circulars.bottomRight))
        addQuadCurve(to: CGPoint(x: rect.width - circulars.bottomRight, y: rect.height), control: CGPoint(x: rect.width, y: rect.height))
        
        addLine(to: CGPoint(x: circulars.bottomLeft, y: rect.height))
        addQuadCurve(to: CGPoint(x: rect.minX, y: rect.height - circulars.bottomLeft), control: CGPoint(x: rect.minX, y: rect.height))
        
        addLine(to: CGPoint(x: rect.minX, y: circulars.topLeft))
        addQuadCurve(to: CGPoint(x: circulars.topLeft, y: rect.minY), control: CGPoint(x: rect.minX, y: rect.minY))
        closeSubpath()
    }
}

/** 圆角视图 */
class MTCircularView: UIView {
    var radius:CGFloat! {
        didSet {
            self.allRadius(radius)
        }
    }
    var radiusLayer:CAShapeLayer {
        return self.layer as! CAShapeLayer
    }
    override class var layerClass: AnyClass {
        return CAShapeLayer.self
    }
    var radiuses:MTCirculars = .zero
    override func layoutSubviews() {
        super.layoutSubviews()
        let path = CGMutablePath()
        path.addRadiusRectangle(radiuses, rect: bounds)
        radiusLayer.path = path
        radiusLayer.shadowPath = path
    }
    /** 顶部圆角 */
    func topRadius(_ size: CGFloat) {
        radiuses = MTCirculars(topLeft: size, topRight: size, bottomLeft: 0, bottomRight: 0)
    }
    /** 底部圆角 */
    func bottomRadius(_ size: CGFloat) {
        radiuses = MTCirculars(topLeft: 0, topRight: 0, bottomLeft: size, bottomRight: size)
    }
    /** 左边圆角 */
    func leftRadius(_ size: CGFloat) {
        radiuses = MTCirculars(topLeft: size, topRight: 0, bottomLeft: size, bottomRight: 0)
    }
    /** 右边圆角 */
    func rightRadius(_ size: CGFloat) {
        radiuses = MTCirculars(topLeft: 0, topRight: size, bottomLeft: 0, bottomRight: size)
    }
    /** 所有圆角 */
    func allRadius(_ size: CGFloat) {
        radiuses = MTCirculars(topLeft: size, topRight: size, bottomLeft: size, bottomRight: size)
    }
}

使用方法

  1. 设置圆角
        let demoView = MTCircularView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
        demoView.center = view.center
        demoView.radiusLayer.fillColor = UIColor.orange.cgColor
        // 底部圆角
        // demoView.bottomRadius(20)
        // 顶部圆角
        // demoView.topRadius(20)
        // 左边圆角
        // demoView.leftRadius(20)
        // 右边圆角
        // demoView.rightRadius(20)
        // 自定义圆角
        // demoView.radiuses = MTCirculars(topLeft: 30, topRight: 10, bottomLeft: 10, bottomRight: 150)
        view.addSubview(demoView)

还有一个有意思的东西,根据路径绘制阴影,修改MTCircularView代码:

    override func layoutSubviews() {
        super.layoutSubviews()
        let path = CGMutablePath()
        path.addRadiusRectangle(radiuses, rect: bounds)
        radiusLayer.path = path
//        radiusLayer.shadowPath = path
    }

调用代码

class ViewController: UIViewController {
    let demoView = MTCircularView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
    let sliderView = UISlider(frame: CGRect(x: 10, y: 0, width: 300, height: 20))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        demoView.center = view.center
        demoView.radiusLayer.fillColor = UIColor.clear.cgColor
        demoView.radiusLayer.strokeColor = UIColor.green.cgColor
        demoView.radiusLayer.lineWidth = 2
        demoView.radiusLayer.strokeEnd = 0
        demoView.radiuses = MTCirculars(topLeft: 30, topRight: 10, bottomLeft: 10, bottomRight: 150)
        
        sliderView.addTarget(self, action: #selector(valueChange), for: .valueChanged)
        sliderView.frame.origin.y = demoView.frame.maxY + 100
        
        view.addSubview(demoView)
        view.addSubview(sliderView)
    }
    @objc func valueChange() {
        demoView.radiusLayer.strokeEnd = CGFloat(sliderView.value)
    }
}

Dec-19-2020 23-58-55.gif
  1. 设置描边
        demoView.radiusLayer.strokeColor = UIColor.green.cgColor
        demoView.radiusLayer.lineWidth = 2
  1. 设置阴影
        demoView.layer.shadowColor = UIColor.black.cgColor
        demoView.layer.shadowOffset = CGSize(width: 0, height: 0)
        demoView.layer.shadowOpacity = 1.0
        demoView.layer.shadowRadius = 5;

示例

Simulator Screen Shot - iPhone 11 Pro - 2020-12-19 at 23.42.35.png Simulator Screen Shot - iPhone 11 Pro - 2020-12-19 at 23.43.36.png Simulator Screen Shot - iPhone 11 Pro - 2020-12-19 at 23.44.47.png Simulator Screen Shot - iPhone 11 Pro - 2020-12-19 at 23.44.56.png Simulator Screen Shot - iPhone 11 Pro - 2020-12-19 at 23.50.15.png

总结

在卡片式布局风格的APP中,圆角阴影无处不在,使用非常之广,选择高效的绘制方法非常有必要。通篇博客所使用到的知识点包括:

  1. 指定UIView 的图层类型,重写 layerClass 方法;
  2. CAShapeView 的 stroke、fill、path 等相关属性的使用;
  3. CGMutablePath 的绘制,掌握贝塞尔曲线的原理非常重要;
  4. layoutSubviews 方法的调用时机,每当视图的frame、bounds 发生变化以及视图层级发生改变时都会调用此方法,重写此方法以动态实现圆角路径和阴影的绘制。
上一篇下一篇

猜你喜欢

热点阅读