Last active
March 3, 2020 14:55
-
-
Save anirudhamahale/5f3bab0d89df21728f4959643c761f47 to your computer and use it in GitHub Desktop.
EmptyDataSet
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 EmptyDataSet: UIView { | |
private(set) var parentView: UIView! | |
private(set) var titleLabel: UILabel! | |
private(set) var descriptionLabel: UILabel! | |
private(set) var imageView: UIImageView! | |
private(set) var button: UIButton! | |
private var imageAnimation: CABasicAnimation { | |
let animation = CABasicAnimation(keyPath: "transform") | |
animation.fromValue = NSValue(caTransform3D: CATransform3DIdentity) | |
animation.toValue = NSValue(caTransform3D: CATransform3DMakeRotation(CGFloat(Double.pi/2), 0.0, 0.0, 1.0)) | |
animation.duration = 0.25 | |
animation.isCumulative = true | |
animation.repeatCount = MAXFLOAT | |
animation.isRemovedOnCompletion = false | |
return animation | |
} | |
var margin: CGFloat = 16.0 | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
self.backgroundColor = .white | |
parentView = UIView() | |
parentView.translatesAutoresizingMaskIntoConstraints = false | |
addSubview(parentView) | |
parentView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true | |
parentView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true | |
} | |
required init?(coder: NSCoder) { | |
super.init(coder: coder) | |
} | |
func setProgress(title: String?, description: String?) { | |
var topConstraint: NSLayoutYAxisAnchor? | |
parentView.subviews.forEach { $0.removeFromSuperview() } | |
// Add Image | |
imageView = UIImageView() | |
imageView.translatesAutoresizingMaskIntoConstraints = false | |
imageView.image = UIImage(named: "Oval") | |
parentView.addSubview(imageView) | |
imageView.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true | |
imageView.topAnchor.constraint(equalTo: parentView.topAnchor, constant: 0).isActive = true | |
imageView.layer.add(imageAnimation, forKey: nil) | |
topConstraint = imageView.bottomAnchor | |
if let title = title { | |
// Add Title | |
titleLabel = UILabel() | |
titleLabel.translatesAutoresizingMaskIntoConstraints = false | |
titleLabel.textColor = .black | |
titleLabel.font = UIFont.boldSystemFont(ofSize: 24) | |
titleLabel.text = title | |
parentView.addSubview(titleLabel) | |
titleLabel.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true | |
titleLabel.topAnchor.constraint(equalTo: topConstraint!, constant: margin).isActive = true | |
topConstraint = titleLabel.bottomAnchor | |
} | |
if let description = description { | |
// Add Description | |
descriptionLabel = UILabel() | |
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false | |
descriptionLabel.textColor = .black | |
descriptionLabel.text = description | |
parentView.addSubview(descriptionLabel) | |
descriptionLabel.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true | |
descriptionLabel.topAnchor.constraint(equalTo: topConstraint!, constant: margin).isActive = true | |
topConstraint = descriptionLabel.bottomAnchor | |
} | |
parentView.bottomAnchor.constraint(equalTo: topConstraint!).isActive = true | |
} | |
func setNoData(image: UIImage?, title: String?, description: String?) { | |
var topConstraint = parentView.topAnchor | |
parentView.subviews.forEach { $0.removeFromSuperview() } | |
if let image = image { | |
// Add Image | |
imageView = UIImageView() | |
imageView.translatesAutoresizingMaskIntoConstraints = false | |
imageView.image = image | |
parentView.addSubview(imageView) | |
imageView.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true | |
imageView.topAnchor.constraint(equalTo: topConstraint, constant: 0).isActive = true | |
topConstraint = imageView.bottomAnchor | |
} | |
if let text = title { | |
// Add Title | |
titleLabel = UILabel() | |
titleLabel.translatesAutoresizingMaskIntoConstraints = false | |
titleLabel.textColor = .black | |
titleLabel.font = UIFont.boldSystemFont(ofSize: 24) | |
titleLabel.text = text | |
parentView.addSubview(titleLabel) | |
titleLabel.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true | |
titleLabel.topAnchor.constraint(equalTo: topConstraint, constant: margin).isActive = true | |
topConstraint = titleLabel.bottomAnchor | |
} | |
if let text = description { | |
// Add Description | |
descriptionLabel = UILabel() | |
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false | |
descriptionLabel.textColor = .black | |
descriptionLabel.text = text | |
parentView.addSubview(descriptionLabel) | |
descriptionLabel.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true | |
descriptionLabel.topAnchor.constraint(equalTo: topConstraint, constant: margin).isActive = true | |
topConstraint = descriptionLabel.bottomAnchor | |
} | |
parentView.bottomAnchor.constraint(equalTo: topConstraint).isActive = true | |
} | |
func setFailed(title: String?, description: String?, buttonTitle: String?, action: ()->()) { | |
var topConstraint = parentView.topAnchor | |
parentView.subviews.forEach { $0.removeFromSuperview() } | |
if let text = title { | |
// Add Title | |
titleLabel = UILabel() | |
titleLabel.translatesAutoresizingMaskIntoConstraints = false | |
titleLabel.textColor = .black | |
titleLabel.font = UIFont.boldSystemFont(ofSize: 24) | |
titleLabel.text = text | |
parentView.addSubview(titleLabel) | |
titleLabel.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true | |
titleLabel.topAnchor.constraint(equalTo: topConstraint, constant: margin).isActive = true | |
topConstraint = titleLabel.bottomAnchor | |
} | |
if let text = description { | |
// Add Description | |
descriptionLabel = UILabel() | |
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false | |
descriptionLabel.textColor = .black | |
descriptionLabel.text = text | |
parentView.addSubview(descriptionLabel) | |
descriptionLabel.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true | |
descriptionLabel.topAnchor.constraint(equalTo: topConstraint, constant: margin).isActive = true | |
topConstraint = descriptionLabel.bottomAnchor | |
} | |
if let text = buttonTitle { | |
button = UIButton() | |
button.backgroundColor = .black | |
button.layer.masksToBounds = true | |
button.setTitle(text, for: .normal) | |
button.setTitleColor(.white, for: .normal) | |
button.translatesAutoresizingMaskIntoConstraints = false | |
parentView.addSubview(button) | |
button.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0).isActive = true | |
button.topAnchor.constraint(equalTo: topConstraint, constant: margin).isActive = true | |
button.contentEdgeInsets = UIEdgeInsets(top: 10, left: 36, bottom: 10, right: 36) | |
topConstraint = button.bottomAnchor | |
button.layoutIfNeeded() | |
button.layer.cornerRadius = button.frame.size.height/2 | |
} | |
parentView.bottomAnchor.constraint(equalTo: topConstraint).isActive = true | |
} | |
} |
Author
anirudhamahale
commented
Mar 3, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment