前言:
绘制内阴影,原作者地址:https://www.jianshu.com/p/b4d1a0b6439a
发现了一个大师写的内阴影代码,挺好用的,但是直接复制粘贴有报错,我修改了下报错。
效果图:
上代码!
WLInnerShadow.swift
import UIKit
class WLInnerShadow: CAShapeLayer {
var innerShadowColor: CGColor? = UIColor.black.cgColor {
didSet { setNeedsDisplay() }
}
var innerShadowOffset: CGSize = CGSize(width: 0, height: 0) {
didSet { setNeedsDisplay() }
}
var innerShadowRadius: CGFloat = 8 {
didSet { setNeedsDisplay() }
}
var innerShadowOpacity: Float = 1 {
didSet { setNeedsDisplay() }
}
override init() {
super.init()
initialize()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
func initialize() {
self.masksToBounds = true
self.shouldRasterize = true
self.contentsScale = UIScreen.main.scale
self.rasterizationScale = UIScreen.main.scale
setNeedsDisplay()
}
override func draw(in ctx: CGContext) {
// 设置 Context 属性
// 允许抗锯齿
//CGContextSetAllowsAntialiasing(ctx, true);
ctx.setAllowsAntialiasing(true)
// 允许平滑
//CGContextSetShouldAntialias(ctx, true);
ctx.setShouldAntialias(true)
// 设置插值质量
//CGContextSetInterpolationQuality(ctx, .High);
ctx.interpolationQuality = .high
// 以下为核心代码
// 创建 color space
let colorspace = CGColorSpaceCreateDeviceRGB();
var rect = self.bounds
var radius = self.cornerRadius
// 去除边框的大小
if self.borderWidth != 0 {
//rect = CGRectInset(rect, self.borderWidth, self.borderWidth);
rect = rect.insetBy(dx: borderWidth, dy: borderWidth)
radius -= self.borderWidth
radius = max(radius, 0)
}
// 创建 inner shadow 的镂空路径
let someInnerPath: CGPath = UIBezierPath(roundedRect: rect, cornerRadius: radius).cgPath
ctx.addPath(someInnerPath)
ctx.clip()
// 创建阴影填充区域,并镂空中心
let shadowPath = CGMutablePath()
//let shadowRect = CGRectInset(rect, -rect.size.width, -rect.size.width)
let shadowRect = rect.insetBy(dx: -rect.size.width, dy: -rect.size.width)
//CGPathAddRect(shadowPath, nil, shadowRect)
shadowPath.addRect(shadowRect)
//CGPathAddPath(shadowPath, nil, someInnerPath);
shadowPath.addPath(someInnerPath)
//CGPathCloseSubpath(shadowPath)
shadowPath.closeSubpath()
// 获取填充颜色信息
//let oldComponents: UnsafePointer<CGFloat> = CGColorGetComponents(innerShadowColor)
let oldComponents = innerShadowColor?.components ?? [0, 0, 0, 0]
var newComponents:[CGFloat] = [0, 0, 0, 0]
//let numberOfComponents: Int = CGColorGetNumberOfComponents(innerShadowColor);
let numberOfComponents = innerShadowColor?.numberOfComponents
switch (numberOfComponents){
case 2:
// 灰度
newComponents[0] = oldComponents[0]
newComponents[1] = oldComponents[0]
newComponents[2] = oldComponents[0]
newComponents[3] = oldComponents[1] * CGFloat(innerShadowOpacity)
case 4:
// RGBA
newComponents[0] = oldComponents[0]
newComponents[1] = oldComponents[1]
newComponents[2] = oldComponents[2]
newComponents[3] = oldComponents[3] * CGFloat(innerShadowOpacity)
default: break
}
// 根据颜色信息创建填充色
//let innerShadowColorWithMultipliedAlpha = CGColorCreate(colorspace, newComponents)
let innerShadowColorWithMultipliedAlpha = CGColor(colorSpace: colorspace, components: &newComponents) ?? UIColor.black.cgColor
// 填充阴影
//CGContextSetFillColorWithColor(ctx, innerShadowColorWithMultipliedAlpha)
ctx.setFillColor(innerShadowColorWithMultipliedAlpha)
//CGContextSetShadowWithColor(ctx, innerShadowOffset, innerShadowRadius, innerShadowColorWithMultipliedAlpha)
ctx.setShadow(offset: innerShadowOffset, blur: innerShadowRadius, color: innerShadowColorWithMultipliedAlpha)
//CGContextAddPath(ctx, shadowPath)
ctx.addPath(shadowPath)
//CGContextEOFillPath(ctx)
ctx.fillPath(using: .evenOdd)
}
}
使用示例:
import UIKit
class InnerShadowViewController_3: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myView = UIView(frame: CGRect(x: 0, y: 0, width: 280, height: 280))
myView.layer.backgroundColor = UIColor.init(hex: 0xeeeeee).cgColor
myView.center = self.view.center
myView.layer.cornerRadius = myView.bounds.size.width / 2
myView.layer.shouldRasterize = true
myView.layer.contentsScale = UIScreen.main.scale
myView.layer.rasterizationScale = UIScreen.main.scale
let shadowLayer = WLInnerShadow()
shadowLayer.frame = myView.bounds
shadowLayer.cornerRadius = myView.layer.cornerRadius
shadowLayer.innerShadowOffset = CGSize(width: 4, height: 4)
shadowLayer.innerShadowOpacity = 0.5
shadowLayer.innerShadowRadius = 16
myView.layer.addSublayer(shadowLayer)
self.view.addSubview(myView)
}
}
结语
这个可以设置透明度了。