Created
June 16, 2017 14:50
-
-
Save temoki/039d5fc28e9610a2f79383cce8117737 to your computer and use it in GitHub Desktop.
穴あきビュー
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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