iOS Swift 內(nèi)陰影 2

前言:

繪制內(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è)置透明度了爷辙。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市朦促,隨后出現(xiàn)的幾起案子膝晾,更是在濱河造成了極大的恐慌,老刑警劉巖务冕,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件血当,死亡現(xiàn)場離奇詭異,居然都是意外死亡禀忆,警方通過查閱死者的電腦和手機臊旭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來箩退,“玉大人离熏,你說我怎么就攤上這事〈骼裕” “怎么了滋戳?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長啥刻。 經(jīng)常有香客問我奸鸯,道長,這世上最難降的妖魔是什么可帽? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任府喳,我火速辦了婚禮,結(jié)果婚禮上蘑拯,老公的妹妹穿的比我還像新娘钝满。我一直安慰自己兜粘,他們只是感情好,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布弯蚜。 她就那樣靜靜地躺著孔轴,像睡著了一般。 火紅的嫁衣襯著肌膚如雪碎捺。 梳的紋絲不亂的頭發(fā)上路鹰,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天,我揣著相機與錄音收厨,去河邊找鬼晋柱。 笑死,一個胖子當著我的面吹牛诵叁,可吹牛的內(nèi)容都是我干的雁竞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼拧额,長吁一口氣:“原來是場噩夢啊……” “哼碑诉!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起侥锦,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤进栽,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后恭垦,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體快毛,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年番挺,在試婚紗的時候發(fā)現(xiàn)自己被綠了唠帝。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡建芙,死狀恐怖没隘,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情禁荸,我是刑警寧澤右蒲,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站赶熟,受9級特大地震影響瑰妄,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜映砖,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一间坐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦竹宋、人聲如沸劳澄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽秒拔。三九已至,卻和暖如春飒硅,著一層夾襖步出監(jiān)牢的瞬間砂缩,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工三娩, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留庵芭,地道東北人。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓雀监,卻偏偏與公主長得像双吆,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子滔悉,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容