Skip to content

Instantly share code, notes, and snippets.

@uvolchyk
Created February 7, 2025 22:42
Show Gist options
  • Save uvolchyk/eed63002a5949eb3a10a4139e0f265dd to your computer and use it in GitHub Desktop.
Save uvolchyk/eed63002a5949eb3a10a4139e0f265dd to your computer and use it in GitHub Desktop.
import UIKit
public final class SpatialContainerView: UIVisualEffectView {
private lazy var borderLayer: CAShapeLayer = {
let layer = CAShapeLayer()
layer.strokeColor = UIColor.white.withAlphaComponent(0.2).cgColor
layer.fillColor = UIColor.clear.cgColor
layer.lineWidth = borderWidth
return layer
}()
private lazy var maskLayer: CAShapeLayer = CAShapeLayer()
public override func layoutSubviews() {
super.layoutSubviews()
let path = UIBezierPath(
roundedRect: bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(
width: cornerRadius,
height: cornerRadius
)
)
maskLayer.path = path.cgPath
borderLayer.path = path.cgPath
}
public init(
cornerRadius: CGFloat = 0,
corners: UIRectCorner = .allCorners
) {
self.cornerRadius = cornerRadius
self.corners = corners
super.init(effect: UIBlurEffect(style: .systemUltraThinMaterialDark))
layer.mask = maskLayer
contentView.layer.addSublayer(borderLayer)
}
private let cornerRadius: CGFloat
private let corners: UIRectCorner
var borderWidth: CGFloat = 1.4 {
didSet { borderLayer.lineWidth = borderWidth }
}
required init?(coder: NSCoder) { nil }
}
@uvolchyk
Copy link
Author

uvolchyk commented Feb 7, 2025

Screen.Recording.2025-02-07.at.11.41.44.PM.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment