Skip to content

Instantly share code, notes, and snippets.

@muukii
Created June 15, 2021 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muukii/deae8141143530911efe80cb643a5bb6 to your computer and use it in GitHub Desktop.
Save muukii/deae8141143530911efe80cb643a5bb6 to your computer and use it in GitHub Desktop.
/// For Transition
public final class ComposedCornerView: CodeBasedView {
private let topRightMaskView = UIView()
private let topLeftMaskView = UIView()
private let bottomRightMaskView = UIView()
private let bottomLeftMaskView = UIView()
private let topRightView = UIView()
private let topLeftView = UIView()
private let bottomRightView = UIView()
private let bottomLeftView = UIView()
public init() {
super.init(frame: .zero)
[
topRightMaskView,
topLeftMaskView,
bottomRightMaskView,
bottomLeftMaskView,
].forEach {
$0.backgroundColor = .black
}
[
topRightView,
topLeftView,
bottomRightView,
bottomLeftView,
].forEach {
addSubview($0)
$0.backgroundColor = .black
}
topRightView.layer.maskedCorners = .layerMaxXMinYCorner
topLeftView.layer.maskedCorners = .layerMinXMinYCorner
bottomRightView.layer.maskedCorners = .layerMaxXMaxYCorner
bottomLeftView.layer.maskedCorners = .layerMinXMaxYCorner
topRightView.mask = topRightMaskView
topLeftView.mask = topLeftMaskView
bottomRightView.mask = bottomRightMaskView
bottomLeftView.mask = bottomLeftMaskView
}
public override func layoutSubviews() {
super.layoutSubviews()
[
topRightView,
topLeftView,
bottomRightView,
bottomLeftView,
].forEach {
$0.frame = bounds
}
let width = bounds.width / 2
let height = bounds.height / 2
topRightMaskView.frame = .init(x: width, y: 0, width: width, height: height)
topLeftMaskView.frame = .init(x: 0, y: 0, width: width, height: height)
bottomRightMaskView.frame = .init(x: width, y: height, width: width, height: height)
bottomLeftMaskView.frame = .init(x: 0, y: height, width: width, height: height)
if #available(iOS 13.0, *) {
if bounds.width == bounds.height {
setCornerCurve(.circular)
} else {
setCornerCurve(.continuous)
}
}
}
@available(iOS 13, *)
public func setCornerCurve(_ curve: CALayerCornerCurve) {
[
topRightView,
topLeftView,
bottomRightView,
bottomLeftView,
].forEach {
$0.layer.cornerCurve = curve
}
}
public func setCorner(radius: CGFloat, mask: CACornerMask) {
let actualRadius = radius
if mask.contains(.layerMinXMinYCorner) {
topLeftView.layer.cornerRadius = actualRadius
} else {
topLeftView.layer.cornerRadius = 0
}
if mask.contains(.layerMaxXMinYCorner) {
topRightView.layer.cornerRadius = actualRadius
} else {
topRightView.layer.cornerRadius = 0
}
if mask.contains(.layerMinXMaxYCorner) {
bottomLeftView.layer.cornerRadius = actualRadius
} else {
bottomLeftView.layer.cornerRadius = 0
}
if mask.contains(.layerMaxXMaxYCorner) {
bottomRightView.layer.cornerRadius = actualRadius
} else {
bottomRightView.layer.cornerRadius = 0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment