-
Notifications
You must be signed in to change notification settings - Fork 1
/
SwiftModelView.swift
91 lines (76 loc) · 2.72 KB
/
SwiftModelView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// SwiftModelView.swift
// Pods
//
// Created by Gaurav Bharti on 10/01/19.
//
import UIKit
public class SwiftModelView:UIView {
@IBOutlet private weak var statusImage: UIImageView!
@IBOutlet private weak var headlineLabel: UILabel!
@IBOutlet private weak var subheadLabel: UILabel!
let nibName = "SwiftModelView"
var contentView: UIView!
var timer: Timer?
// Set Up View
public override init(frame: CGRect) {
// For use in code
super.init(frame: frame)
setUpView()
}
public required init?(coder aDecoder: NSCoder) {
// For use in Interface Builder
super.init(coder: aDecoder)
setUpView()
}
private func setUpView() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: self.nibName, bundle: bundle)
self.contentView = (nib.instantiate(withOwner: self, options: nil).first as! UIView)
addSubview(contentView)
contentView.center = self.center
contentView.autoresizingMask = []
contentView.translatesAutoresizingMaskIntoConstraints = true
contentView.alpha = 0.0
headlineLabel.text = ""
subheadLabel.text = ""
}
// Provide functions to update view
public func set(image: UIImage) {
self.statusImage.image = image
}
public func set(headline text: String) {
self.headlineLabel.text = text
}
public func set(subheading text: String) {
self.subheadLabel.text = text
}
// Allow view to control itself
public override func layoutSubviews() {
// Rounded corners
self.layoutIfNeeded()
self.contentView.layer.masksToBounds = true
self.contentView.clipsToBounds = true
self.contentView.layer.cornerRadius = 10
}
public override func didMoveToSuperview() {
// Fade in when added to superview
// Then add a timer to remove the view
self.contentView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
UIView.animate(withDuration: 0.15, animations: {
self.contentView.alpha = 1.0
self.contentView.transform = CGAffineTransform.identity
}) { _ in
self.timer = Timer.scheduledTimer(timeInterval: TimeInterval(3.0), target: self, selector: #selector(self.removeSelf), userInfo: nil, repeats: false)
}
}
@objc private func removeSelf() {
// Animate removal of view
UIView.animate(withDuration: 0.15, animations: {
self.contentView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
self.contentView.alpha = 0.0
}) { _ in
self.removeFromSuperview()
}
}
}