Created
July 3, 2017 15:03
-
-
Save JimSpriggs/9085909d9e4cd56de42cc9bbfc8fb0d2 to your computer and use it in GitHub Desktop.
Toast equivalent for iOS
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 ToastView: UIView { | |
private static let hLabelGap: CGFloat = 40.0 | |
private static let vLabelGap: CGFloat = 20.0 | |
private static let hToastGap: CGFloat = 20.0 | |
private static let vToastGap: CGFloat = 10.0 | |
private var textLabel: UILabel! | |
static func showInParent(_ parentView: UIView, _ text: String, duration: Double = 3.0) { | |
let labelFrame = CGRect(x: parentView.frame.origin.x + hLabelGap, | |
y: parentView.frame.origin.y + vLabelGap, | |
width: parentView.frame.width - 2 * hLabelGap, | |
height: parentView.frame.height - 2 * vLabelGap) | |
let label = UILabel() | |
label.font = UIFont.systemFont(ofSize: 15.0) | |
label.text = text | |
label.backgroundColor = UIColor.clear | |
label.textAlignment = NSTextAlignment.center | |
label.textColor = UIColor.white | |
label.numberOfLines = 0 | |
label.frame = labelFrame | |
label.sizeToFit() | |
let toast = ToastView() | |
toast.textLabel = label | |
toast.addSubview(label) | |
toast.frame = CGRect(x: label.frame.origin.x - hToastGap, | |
y: label.frame.origin.y - vToastGap, | |
width: label.frame.width + 2 * hToastGap, | |
height: label.frame.height + 2 * vToastGap) | |
toast.backgroundColor = UIColor.darkGray | |
toast.alpha = 0.0 | |
toast.layer.cornerRadius = 20.0 | |
toast.center = parentView.center | |
label.center = CGPoint(x: toast.frame.size.width / 2, y: toast.frame.size.height / 2) | |
parentView.addSubview(toast) | |
UIView.animate(withDuration: 0.4, animations: { | |
toast.alpha = 0.9 | |
label.alpha = 0.9 | |
}) | |
toast.perform(#selector(hideSelf), with: nil, afterDelay: duration) | |
} | |
@objc private func hideSelf() { | |
UIView.animate(withDuration: 0.4, animations: { | |
self.alpha = 0.0 | |
self.textLabel.alpha = 0.0 | |
}, completion: { t in self.removeFromSuperview() }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment