Skip to content

Instantly share code, notes, and snippets.

@zakkhoyt
Created August 1, 2017 19:16
Show Gist options
  • Select an option

  • Save zakkhoyt/9e4fe6a411ed7b5ed0618a162bd73cd6 to your computer and use it in GitHub Desktop.

Select an option

Save zakkhoyt/9e4fe6a411ed7b5ed0618a162bd73cd6 to your computer and use it in GitHub Desktop.
A demo to show different blending modes using CGBlendMod
//
// A demo to show different blending modes using CGBlendMod
//
import UIKit
extension CGBlendMode {
static func count() -> Int {
return 16
}
func toString() -> String {
switch self {
case .normal:
return "normal"
case .multiply:
return "multiply"
case .screen:
return "screen"
case .overlay:
return "overlay"
case .darken:
return "darken"
case .lighten:
return "lighten"
case .colorDodge:
return "colorDodge"
case .colorBurn:
return "colorBurn"
case .softLight:
return "softLight"
case .hardLight:
return "hardLight"
case .difference:
return "difference"
case .exclusion:
return "exclusion"
case .hue:
return "hue"
case .saturation:
return "saturation"
case .color:
return "color"
case .luminosity:
return "luminosity"
default:
return "Unknown"
}
}
}
class BlendView: UIView {
var blendMode: CGBlendMode = .normal {
didSet {
setNeedsDisplay()
}
}
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else {
return
}
context.setBlendMode(self.blendMode)
context.setLineWidth(1.0)
context.setStrokeColor(UIColor.clear.cgColor)
context.setFillColor(UIColor.lightGray.cgColor)
// Background
do {
let path = UIBezierPath(rect: bounds).cgPath
context.addPath(path)
context.fillPath()
context.strokePath()
}
context.setStrokeColor(UIColor.black.cgColor)
context.setFillColor(UIColor.green.withAlphaComponent(0.2).cgColor)
// Draw some colored rects
do {
let x: CGFloat = 0
let y: CGFloat = 0
let width: CGFloat = 200
let height: CGFloat = 200
let frame = CGRect(x: x, y: y, width: width, height: height)
let path = UIBezierPath(rect: frame).cgPath
context.addPath(path)
context.fillPath()
context.strokePath()
}
do {
let x: CGFloat = 100
let y: CGFloat = 100
let width: CGFloat = 200
let height: CGFloat = 200
let frame = CGRect(x: x, y: y, width: width, height: height)
let path = UIBezierPath(rect: frame).cgPath
context.addPath(path)
context.fillPath()
context.strokePath()
}
// Change color and draw more rects
context.setFillColor(UIColor.red.withAlphaComponent(0.2).cgColor)
do {
let x: CGFloat = 0
let y: CGFloat = 150
let width: CGFloat = 200
let height: CGFloat = 200
let frame = CGRect(x: x, y: y, width: width, height: height)
let path = UIBezierPath(rect: frame).cgPath
context.addPath(path)
context.fillPath()
context.strokePath()
}
do {
let x: CGFloat = 0
let y: CGFloat = 250
let width: CGFloat = 200
let height: CGFloat = 200
let frame = CGRect(x: x, y: y, width: width, height: height)
let path = UIBezierPath(rect: frame).cgPath
context.addPath(path)
context.fillPath()
context.strokePath()
}
}
}
class ViewController: UIViewController {
private var stackView: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
setupStackView()
}
private func setupStackView() {
let width: CGFloat = 128
let height: CGFloat = view.bounds.height
let x: CGFloat = view.bounds.width - width
let y: CGFloat = 0
let frame = CGRect(x: x, y: y, width: width, height: height)
stackView = UIStackView(frame: frame)
stackView.axis = .vertical
stackView.distribution = .fillEqually
view.addSubview(stackView)
for i in 0..<CGBlendMode.count() {
let blendMode = CGBlendMode(rawValue: Int32(i))
let button = UIButton(type: .custom)
button.tag = i
button.setTitle(blendMode?.toString(), for: .normal)
button.setTitleColor(UIColor.black, for: .normal)
button.addTarget(self, action: #selector(blendFilterButtonTouchUpInside), for: .touchUpInside)
stackView.addArrangedSubview(button)
}
}
@objc private func blendFilterButtonTouchUpInside(sender: UIButton) {
guard let blendView = self.view as? BlendView else {
return
}
let tag = sender.tag
guard let blendMode = CGBlendMode(rawValue: Int32(tag)) else {
return
}
blendView.blendMode = blendMode
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment