前言:
繪制內(nèi)陰影,原作者地址:http://www.reibang.com/p/b4d1a0b6439a
發(fā)現(xiàn)了一個大師寫的內(nèi)陰影代碼,挺好用的空凸,但是直接復制粘貼有報錯道逗,我修改了下報錯裆赵。
效果圖:
內(nèi)陰影.PNG
上代碼!
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) {
// 設(shè)置 Context 屬性
// 允許抗鋸齒
//CGContextSetAllowsAntialiasing(ctx, true);
ctx.setAllowsAntialiasing(true)
// 允許平滑
//CGContextSetShouldAntialias(ctx, true);
ctx.setShouldAntialias(true)
// 設(shè)置插值質(zhì)量
//CGContextSetInterpolationQuality(ctx, .High);
ctx.interpolationQuality = .high
// 以下為核心代碼
// 創(chuàng)建 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)
}
// 創(chuàng)建 inner shadow 的鏤空路徑
let someInnerPath: CGPath = UIBezierPath(roundedRect: rect, cornerRadius: radius).cgPath
ctx.addPath(someInnerPath)
ctx.clip()
// 創(chuàng)建陰影填充區(qū)域肚逸,并鏤空中心
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
}
// 根據(jù)顏色信息創(chuàng)建填充色
//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)
}
}
結(jié)語
這個可以設(shè)置透明度了爷辙。