Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public final class BlurryBackgroundButton: UIControl {
public init() {
super.init(frame: .zero)
setup()
}
public override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setup() {
backgroundColor = UIColor(white: 247/255, alpha: 0.8)
layer.shadowColor = UIColor(white: 0, alpha: 1).cgColor
layer.shadowOffset = CGSize(width: 0, height: 4)
layer.shadowRadius = 8
layer.shadowOpacity = 0.15
layer.cornerRadius = 8
stackView.layoutMargins = UIEdgeInsets(top: 12, left: 8, bottom: 12, right: 8)
stackView.isLayoutMarginsRelativeArrangement = true
add(view: blurView)
.lockFrame(withView: self)
.lockToCenter(ofView: self)
add(view: stackView)
.lockToCenter(ofView: self)
.lockToHeight(ofView: self)
addTarget(self, action: #selector(touchDown), for: [.touchDown, .touchDragEnter])
addTarget(self, action: #selector(touchUp), for: [.touchUpInside, .touchDragExit, .touchCancel])
}
private lazy var blurView: UIVisualEffectView = {
let blur = UIBlurEffect(style: .light)
let view = UIVisualEffectView(effect: blur).forAutoLayout()
view.layer.cornerRadius = 8
view.layer.masksToBounds = true
return view
}()
private lazy var stackView: UIStackView = .horizontalStackView(with: [imageView, titleLabel], spacing: 8)
private lazy var imageView: UIImageView = {
let image = Asset.Icons.editCalender.image.withRenderingMode(.alwaysTemplate)
let imageView = UIImageView(image: image).forAutoLayout()
imageView.tintColor = .black
imageView.contentMode = .scaleAspectFit
return imageView
}()
private lazy var titleLabel: UILabel = {
let label = UILabel()
label.text = "Start Date"
label.font = UIFont(mealPlanFont: .demiBold, ofSize: 17)
return label
}()
// MARK: Touch Handling
private var animator = UIViewPropertyAnimator()
@objc private func touchDown(sender: Any, forEvent: UIEvent) {
animator.stopAnimation(true)
transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
layer.shadowOffset = [0, 2]
layer.shadowOpacity = 0.5
}
@objc private func touchUp() {
animator = UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut, animations: { [weak self] in
self?.transform = .identity
self?.layer.shadowOffset = [0, 4]
self?.layer.shadowOpacity = 0.15
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.