-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplashViewProtocol.swift
75 lines (62 loc) · 3.2 KB
/
SplashViewProtocol.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
//
// SplashViewProtocol.swift
// SplashViewTest
//
// Created by hanwe on 2021/01/17.
//
import Foundation
import UIKit
protocol SplashViewProtocol: class {
var targetView: UIView? { get set }
var attachedView: UIView? { get set }
func showSplashView(fadeInDuration: TimeInterval, completion: (() -> ())?)
func hideSplashView(fadeOutDuration: TimeInterval, completion: (() -> ())?)
}
extension SplashViewProtocol {
func showSplashView(fadeInDuration: TimeInterval = 0.0, completion: (() -> ())?) {
guard let attached = self.attachedView else { print("attachedView is null") ; return }
guard let target = self.targetView else { print("targetView is null") ; return }
target.addSubview(attached)
attached.translatesAutoresizingMaskIntoConstraints = false
let constraint1 = NSLayoutConstraint(item: attached, attribute: .leading, relatedBy: .equal,
toItem: target, attribute: .leading,
multiplier: 1.0, constant: 0)
let constraint2 = NSLayoutConstraint(item: attached, attribute: .trailing, relatedBy: .equal,
toItem: target, attribute: .trailing,
multiplier: 1.0, constant: 0)
let constraint3 = NSLayoutConstraint(item: attached, attribute: .top, relatedBy: .equal,
toItem: target, attribute: .top,
multiplier: 1.0, constant: 0)
let constraint4 = NSLayoutConstraint(item: attached, attribute: .bottom, relatedBy: .equal,
toItem: target, attribute: .bottom,
multiplier: 1.0, constant: 0)
target.addConstraints([constraint1, constraint2, constraint3, constraint4])
fadeIn(duration: fadeInDuration, completion: {
completion?()
})
}
func hideSplashView(fadeOutDuration: TimeInterval = 0.3, completion: (() -> ())?) {
guard let attached = self.attachedView else { print("attachedView is null") ; return }
guard let _ = self.targetView else { print("targetView is null") ; return }
fadeOut(duration: fadeOutDuration, completion: {
attached.removeFromSuperview()
completion?()
})
}
fileprivate func fadeIn(duration: TimeInterval, completion: @escaping () -> ()) {
self.attachedView?.alpha = 0.0
UIView.animate(withDuration: duration, delay: 0, options: .curveEaseIn, animations: { [weak self] in
self?.attachedView?.alpha = 1.0
}, completion: { (finished: Bool) -> Void in
completion()
})
}
fileprivate func fadeOut(duration: TimeInterval, completion: @escaping () -> ()) {
self.attachedView?.alpha = 1.0
UIView.animate(withDuration: duration, delay: 0, options: .curveEaseOut, animations: { [weak self] in
self?.attachedView?.alpha = 0.0
}, completion: { (finished: Bool) -> Void in
completion()
})
}
}