Created
November 6, 2020 04:03
-
-
Save trilliwon/86d18fba1a3425c66815a63b4cb315d9 to your computer and use it in GitHub Desktop.
Tooltipview for button descirptions
This file contains hidden or 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 | |
import Shared | |
class TooltipView: UIView { | |
var message: String? { | |
didSet { label.text = message } | |
} | |
var actionWithHide: (() -> Void)? | |
let triangle: TriangleView = { | |
$0.translatesAutoresizingMaskIntoConstraints = false | |
$0.backgroundColor = .clear | |
return $0 | |
}(TriangleView()) | |
let button: UIButton = { | |
$0.translatesAutoresizingMaskIntoConstraints = false | |
$0.backgroundColor = UIColor.mhk.basicBlue | |
$0.layer.cornerRadius = 4 | |
$0.clipsToBounds = true | |
$0.isUserInteractionEnabled = true | |
return $0 | |
}(UIButton()) | |
let label: UILabel = { | |
$0.translatesAutoresizingMaskIntoConstraints = false | |
$0.sizeToFit() | |
$0.textAlignment = .center | |
$0.font = Font.SfCompactDisplay.regular.withSize(12) | |
$0.textColor = .white | |
$0.numberOfLines = 2 | |
$0.setContentCompressionResistancePriority(.required, for: .horizontal) | |
return $0 | |
}(UILabel()) | |
let cancelImageView: UIImageView = { | |
$0.translatesAutoresizingMaskIntoConstraints = false | |
$0.image = UIImage(named: "tooltipCancel") | |
$0.contentMode = .scaleAspectFit | |
return $0 | |
}(UIImageView()) | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
layout() | |
button.addTarget(self, action: #selector(contentViewTapped), for: .touchUpInside) | |
button.add(haptic: .impact(.light)) | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
private func layout() { | |
backgroundColor = .clear | |
addSubview(triangle) | |
addSubview(button) | |
button.addSubview(label) | |
button.addSubview(cancelImageView) | |
NSLayoutConstraint.activate([ | |
triangle.topAnchor.constraint(equalTo: topAnchor), | |
triangle.heightAnchor.constraint(equalToConstant: 8), | |
triangle.widthAnchor.constraint(equalToConstant: 8), | |
button.topAnchor.constraint(equalTo: triangle.bottomAnchor), | |
button.heightAnchor.constraint(equalToConstant: 41), | |
button.leftAnchor.constraint(equalTo: leftAnchor), | |
button.rightAnchor.constraint(equalTo: rightAnchor), | |
label.centerYAnchor.constraint(equalTo: button.centerYAnchor), | |
label.rightAnchor.constraint(equalTo: cancelImageView.rightAnchor, constant: -15), | |
label.leftAnchor.constraint(equalTo: button.leftAnchor, constant: 20), | |
cancelImageView.widthAnchor.constraint(equalToConstant: 7), | |
cancelImageView.heightAnchor.constraint(equalToConstant: 7), | |
cancelImageView.topAnchor.constraint(equalTo: label.topAnchor), | |
cancelImageView.rightAnchor.constraint(equalTo: rightAnchor, constant: -9) | |
]) | |
} | |
func show(superView: UIView, toBottomView: UIView, tailCenterXAnchor: NSLayoutXAxisAnchor? = nil) { | |
removeFromSuperview() | |
superView.addSubview(self) | |
let centerAutoLayout = centerXAnchor.constraint(equalTo: toBottomView.centerXAnchor) | |
centerAutoLayout.priority = .defaultLow | |
centerAutoLayout.isActive = true | |
let rightAutoLayout = rightAnchor.constraint(lessThanOrEqualTo: superView.rightAnchor, constant: -15) | |
rightAutoLayout.priority = .required | |
rightAutoLayout.isActive = true | |
topAnchor.constraint(equalTo: toBottomView.bottomAnchor).isActive = true | |
if let centerXAnchor = tailCenterXAnchor { | |
triangle.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true | |
} else { | |
triangle.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true | |
} | |
} | |
func hide() { | |
UIView.transition( | |
with: self, | |
duration: 0.3, | |
options: .curveEaseIn, | |
animations: { [weak self] in self?.alpha = .zero }, | |
completion: { [weak self] _ in | |
self?.removeFromSuperview() | |
}) | |
actionWithHide?() | |
} | |
@objc | |
func contentViewTapped() { | |
hide() | |
} | |
class TriangleView: UIView { | |
override func draw(_ rect: CGRect) { | |
guard let context = UIGraphicsGetCurrentContext() else { return } | |
context.beginPath() | |
context.move(to: CGPoint(x: rect.midX, y: rect.minY)) | |
context.addLine(to: CGPoint(x: rect.minX, y: rect.maxY)) | |
context.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY)) | |
context.closePath() | |
context.setFillColor(UIColor.mhk.basicBlue.cgColor) | |
context.fillPath() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment