Skip to content

Instantly share code, notes, and snippets.

@temoki
Created June 16, 2017 14:50
Show Gist options
  • Save temoki/039d5fc28e9610a2f79383cce8117737 to your computer and use it in GitHub Desktop.
Save temoki/039d5fc28e9610a2f79383cce8117737 to your computer and use it in GitHub Desktop.
穴あきビュー
import UIKit
class HoleView: UIView {
override func awakeFromNib() {
super.awakeFromNib()
}
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else { return }
// 穴の外側を塗りつぶす
UIColor.black.withAlphaComponent(0.5).setFill()
context.fill(rect)
// 穴はブレンドモードで .clear を指定して塗りつぶす
let holeRect = rect.insetBy(dx: rect.width * 0.25, dy: (rect.height - rect.width * 0.5) * 0.5)
context.setBlendMode(.clear)
context.fill(holeRect)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment