-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIView+Utility
60 lines (50 loc) · 2.03 KB
/
UIView+Utility
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
extension UIView {
class func nib<T: UIView>(owner: Any? = nil) -> T {
return Bundle.main.loadNibNamed(String(describing: T.self), owner: owner, options: nil)![0] as! T
}
static var className: String {
return String(describing: self)
}
func makeDashedBorder(color: UIColor = UIColor.gray,
pattern: [NSNumber] = [6, 3],
cornerRadius: CGFloat = 0.0,
lineWidth: CGFloat = 1.0) {
let borderLayer = CAShapeLayer()
borderLayer.strokeColor = color.cgColor
borderLayer.lineDashPattern = pattern
borderLayer.cornerRadius = cornerRadius
borderLayer.lineWidth = lineWidth
borderLayer.frame = bounds
borderLayer.fillColor = nil
borderLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath
layer.addSublayer(borderLayer)
}
func shake() {
let shake = CABasicAnimation(keyPath: "position")
let xDelta = CGFloat(5)
shake.duration = 0.05
shake.repeatCount = 4
shake.autoreverses = true
let from_point = CGPoint(x: self.center.x - xDelta, y: self.center.y)
let from_value = NSValue(cgPoint: from_point)
let to_point = CGPoint(x: self.center.x + xDelta, y: self.center.y)
let to_value = NSValue(cgPoint: to_point)
shake.fromValue = from_value
shake.toValue = to_value
shake.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
self.layer.add(shake, forKey: "position")
}
func fadeOut(withDuration duration: TimeInterval = 0.2) {
UIView.animate(withDuration: duration, animations: {
self.alpha = 0.0
}) { finished in
self.isHidden = true
}
}
func fadeIn(withDuration duration: TimeInterval = 0.2) {
isHidden = false
UIView.animate(withDuration: duration) {
self.alpha = 1.0
}
}
}