CoreGraphic框架解析 (十三)—— Shadows 和 Gloss (二)

版本記錄

版本號(hào) 時(shí)間
V1.0 2019.02.11 星期一

前言

quartz是一個(gè)通用的術(shù)語(yǔ)躬窜,用于描述在iOSMAC OS X 中整個(gè)媒體層用到的多種技術(shù) 包括圖形羹幸、動(dòng)畫(huà)、音頻、適配翻擒。Quart 2D 是一組二維繪圖和渲染API宠进,Core Graphic會(huì)使用到這組API可训,Quartz Core專指Core Animation用到的動(dòng)畫(huà)相關(guān)的庫(kù)淤堵、API和類。CoreGraphicsUIKit下的主要繪圖系統(tǒng)柬批,頻繁的用于繪制自定義視圖啸澡。Core Graphics是高度集成于UIView和其他UIKit部分的。Core Graphics數(shù)據(jù)結(jié)構(gòu)和函數(shù)可以通過(guò)前綴CG來(lái)識(shí)別氮帐。在app中很多時(shí)候繪圖等操作我們要利用CoreGraphic框架嗅虏,它能繪制字符串、圖形上沐、漸變色等等皮服,是一個(gè)很強(qiáng)大的工具。感興趣的可以看我另外幾篇参咙。
1. CoreGraphic框架解析(一)—— 基本概覽
2. CoreGraphic框架解析(二)—— 基本使用
3. CoreGraphic框架解析(三)—— 類波浪線的實(shí)現(xiàn)
4. CoreGraphic框架解析(四)—— 基本架構(gòu)補(bǔ)充
5. CoreGraphic框架解析 (五)—— 基于CoreGraphic的一個(gè)簡(jiǎn)單繪制示例 (一)
6. CoreGraphic框架解析 (六)—— 基于CoreGraphic的一個(gè)簡(jiǎn)單繪制示例 (二)
7. CoreGraphic框架解析 (七)—— 基于CoreGraphic的一個(gè)簡(jiǎn)單繪制示例 (三)
8. CoreGraphic框架解析 (八)—— 基于CoreGraphic的一個(gè)簡(jiǎn)單繪制示例 (四)
9. CoreGraphic框架解析 (九)—— 一個(gè)簡(jiǎn)單小游戲 (一)
10. CoreGraphic框架解析 (十)—— 一個(gè)簡(jiǎn)單小游戲 (二)
11. CoreGraphic框架解析 (十一)—— 一個(gè)簡(jiǎn)單小游戲 (三)
12. CoreGraphic框架解析 (十二)—— Shadows 和 Gloss (一)

源碼

1. Swift

首先看下工程組織結(jié)構(gòu)

看一下sb中的內(nèi)容

下面就是源碼了

1. CoolTableViewController.swift
import UIKit

class CoolTableViewController: UITableViewController {
  let thingsToLearn = ["Drawing Rects", "Drawing Gradients", "Drawing Arcs"]
  let thingsLearned = ["Table Views", "UIKit", "Swift"]

  override func numberOfSections(in tableView: UITableView) -> Int {
    return 2
  }
  
  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return section == 0 ? thingsToLearn.count : thingsLearned.count
  }
  
  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    
    if cell.backgroundView?.isKind(of: CustomCellBackground.self) != true {
      cell.backgroundView = CustomCellBackground()
    }
    
    if cell.selectedBackgroundView?.isKind(of: CustomCellBackground.self) != true {
      cell.selectedBackgroundView = CustomCellBackground()
    }
    
    cell.textLabel?.text = indexPath.section == 0 ? thingsToLearn[indexPath.row] : thingsLearned[indexPath.row]
    
    return cell
  }
  
  override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 50
  }
  
  override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    guard let customHeaderView = CustomHeader.loadViewFromNib() else {
      return nil
    }

    customHeaderView.titleLabel.text = self.tableView(tableView, titleForHeaderInSection: section)
    
    if section == 1 {
      customHeaderView.lightColor = UIColor(red: 147/255.0, green: 105/255.0, blue: 216/255.0, alpha: 1)
      customHeaderView.darkColor = UIColor(red: 72/255.0, green: 22/255.0, blue: 137/255.0, alpha: 1)
    }
    
    return customHeaderView
  }
  
  override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return section == 0 ? "Things We'll Learn" : "Things Already Covered"
  }
}
2. CustomCellBackground.swift
import UIKit

class CustomCellBackground: UIView {
  let lightGrayColor = UIColor(red: 230/255.0, green: 230/255.0, blue: 230/255.0, alpha: 1)
  let separatorColor = UIColor(red: 208/255.0, green: 208/255.0, blue: 208/255.0, alpha: 1)
  
  override func draw(_ rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()!
    
    context.drawLinearGradient(rect: bounds, startColor: UIColor.white, endColor: lightGrayColor)
    
    var strokeRect = bounds
    context.setStrokeColor(UIColor.white.cgColor)
    context.setLineWidth(1)
    strokeRect.size.height -= 1
    context.stroke(strokeRect.rectFor1PxStroke())
    
    let startPoint = CGPoint(x: bounds.origin.x, y: bounds.origin.y + bounds.size.height - 1)
    let endPoint = CGPoint(x: bounds.origin.x + bounds.width - 1, y: bounds.origin.y + bounds.size.height - 1)
    
    context.draw1PxStroke(startPoint: startPoint, endPoint: endPoint, color: separatorColor)
  }
}
3. CustomHeader.swift
import UIKit

class CustomHeader: UIView {
  @IBOutlet public var titleLabel: UILabel!
  var lightColor = UIColor(red: 105/255.0, green: 179/255.0, blue: 216/255.0, alpha: 1)
  var darkColor = UIColor(red: 21/255.0, green: 92/255.0, blue: 136/255.0, alpha: 1)
  
  @IBInspectable var coloredBoxHeight: CGFloat = 40
  
  class func loadViewFromNib() -> CustomHeader? {
    let bundle = Bundle.main
    let nib = UINib(nibName: "CustomHeader", bundle: bundle)
    guard
      let view = nib.instantiate(withOwner: CustomHeader())
        .first as? CustomHeader
      else {
        return nil
    }
    
    return view
  }
  
  override func draw(_ rect: CGRect) {
    // Drawing code
    var coloredBoxRect = bounds
    coloredBoxRect.size.height = coloredBoxHeight
    
    var paperRect = bounds
    paperRect.origin.y += coloredBoxHeight
    paperRect.size.height = bounds.height - coloredBoxHeight
    
    let context = UIGraphicsGetCurrentContext()!
    
//    context.setFillColor(UIColor.red.cgColor)
//    context.fill(coloredBoxRect)
//    
//    context.setFillColor(UIColor.green.cgColor)
//    context.fill(paperRect)
    
    let shadowColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 0.5)
    
    context.saveGState()
    context.setShadow(offset: CGSize(width: 0, height: 2), blur: 3.0, color: shadowColor.cgColor)
    context.setFillColor(lightColor.cgColor)
    context.fill(coloredBoxRect)
    context.restoreGState()
    
    context.drawGlossAndGradient(rect: coloredBoxRect, startColor: lightColor, endColor: darkColor)
    context.setStrokeColor(darkColor.cgColor)
    context.setLineWidth(1)
    context.stroke(coloredBoxRect.rectFor1PxStroke())
  }
}
4. Extensions.swift
import UIKit

extension CGContext {
  func drawLinearGradient(rect: CGRect, startColor: UIColor, endColor: UIColor) {
    let gradient = CGGradient(colorsSpace: nil, colors: [startColor.cgColor, endColor.cgColor] as CFArray, locations: [0, 1])!
    
    let startPoint = CGPoint(x: rect.midX, y: rect.minY)
    let endPoint = CGPoint(x: rect.midX, y: rect.maxY)
    
    saveGState()
    addRect(rect)
    clip()
    drawLinearGradient(gradient, start: startPoint, end: endPoint, options: [])
    
    restoreGState()
  }
  
  func draw1PxStroke(startPoint: CGPoint, endPoint: CGPoint, color: UIColor) {
    saveGState()
    setLineCap(.square)
    setStrokeColor(color.cgColor)
    setLineWidth(1)
    move(to: startPoint+0.5)
    addLine(to: endPoint)
    strokePath()
    restoreGState()
  }
  
  func drawGlossAndGradient(rect: CGRect, startColor: UIColor, endColor: UIColor) {
    drawLinearGradient(rect: rect, startColor: startColor, endColor: endColor)
    
    let glossColor1 = UIColor.white.withAlphaComponent(0.35)
    let glossColor2 = UIColor.white.withAlphaComponent(0.1)
    
    var topHalf = rect
    topHalf.size.height /= 2
    
    drawLinearGradient(rect: topHalf, startColor: glossColor1, endColor: glossColor2)
  }
}

extension CGRect {
  func rectFor1PxStroke() -> CGRect {
    return CGRect(x: origin.x + 0.5, y: origin.y + 0.5, width: size.width - 1, height: size.height - 1)
  }
}


extension CGPoint {
  static func +(left: CGPoint, right: CGFloat) -> CGPoint {
    return CGPoint(x: left.x + right, y: left.y + right)
  }
}

后記

本篇主要講述了Shadows 和 Gloss龄广,感興趣的給個(gè)贊或者關(guān)注~~~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市蕴侧,隨后出現(xiàn)的幾起案子择同,更是在濱河造成了極大的恐慌,老刑警劉巖净宵,帶你破解...
    沈念sama閱讀 219,539評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件敲才,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡择葡,警方通過(guò)查閱死者的電腦和手機(jī)紧武,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)刁岸,“玉大人脏里,你說(shuō)我怎么就攤上這事她我『缡铮” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,871評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵番舆,是天一觀的道長(zhǎng)酝碳。 經(jīng)常有香客問(wèn)我,道長(zhǎng)恨狈,這世上最難降的妖魔是什么疏哗? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,963評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮禾怠,結(jié)果婚禮上返奉,老公的妹妹穿的比我還像新娘贝搁。我一直安慰自己,他們只是感情好芽偏,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評(píng)論 6 393
  • 文/花漫 我一把揭開(kāi)白布雷逆。 她就那樣靜靜地躺著,像睡著了一般污尉。 火紅的嫁衣襯著肌膚如雪膀哲。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,763評(píng)論 1 307
  • 那天被碗,我揣著相機(jī)與錄音某宪,去河邊找鬼。 笑死锐朴,一個(gè)胖子當(dāng)著我的面吹牛兴喂,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播包颁,決...
    沈念sama閱讀 40,468評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼瞻想,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了娩嚼?” 一聲冷哼從身側(cè)響起蘑险,我...
    開(kāi)封第一講書(shū)人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎岳悟,沒(méi)想到半個(gè)月后佃迄,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,850評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贵少,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評(píng)論 3 338
  • 正文 我和宋清朗相戀三年呵俏,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片滔灶。...
    茶點(diǎn)故事閱讀 40,144評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡普碎,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出录平,到底是詐尸還是另有隱情麻车,我是刑警寧澤,帶...
    沈念sama閱讀 35,823評(píng)論 5 346
  • 正文 年R本政府宣布斗这,位于F島的核電站动猬,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏表箭。R本人自食惡果不足惜赁咙,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧彼水,春花似錦崔拥、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,026評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至叛赚,卻和暖如春澡绩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背俺附。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,150評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工肥卡, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人事镣。 一個(gè)月前我還...
    沈念sama閱讀 48,415評(píng)論 3 373
  • 正文 我出身青樓步鉴,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親璃哟。 傳聞我的和親對(duì)象是個(gè)殘疾皇子氛琢,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評(píng)論 2 355

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