-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathUIView+Extensions.swift
57 lines (48 loc) · 1.54 KB
/
UIView+Extensions.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
//
// UIView+Extensions.swift
// GraphTest
//
// Created by Andrei Salavei on 4/10/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import UIKit
extension UIView {
static let oneDevicePixel: CGFloat = (1.0 / max(2, min(1, UIScreen.main.scale)))
}
// MARK: UIView+Animation
public extension UIView {
func bringToFront() {
superview?.bringSubviewToFront(self)
}
func layoutIfNeeded(animated: Bool) {
UIView.perform(animated: animated) {
self.layoutIfNeeded()
}
}
func setVisible(_ visible: Bool, animated: Bool) {
let updatedAlpha: CGFloat = visible ? 1 : 0
if self.alpha != updatedAlpha {
UIView.perform(animated: animated) {
self.alpha = updatedAlpha
}
}
}
static func perform(animated: Bool, animations: @escaping () -> Void) {
perform(animated: animated, animations: animations, completion: { _ in })
}
static func perform(animated: Bool, animations: @escaping () -> Void, completion: @escaping (Bool) -> Void) {
if animated {
UIView.animate(withDuration: .defaultDuration, delay: 0, animations: animations, completion: completion)
} else {
animations()
completion(true)
}
}
var isVisibleInWindow: Bool {
guard let windowBounds = window?.bounds else {
return false
}
let frame = convert(bounds, to: nil)
return frame.intersects(windowBounds)
}
}