-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathContent.swift
More file actions
125 lines (104 loc) · 3.65 KB
/
Content.swift
File metadata and controls
125 lines (104 loc) · 3.65 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import UIKit
public final class Content: NSObject {
public var view: UIView
public var centered: Bool
public var position: Position {
didSet {
layout()
}
}
public private(set) var initialPosition: Position
private var constraints = [NSLayoutConstraint]()
public init(view: UIView, position: Position, centered: Bool = true) {
self.view = view
self.position = position
self.centered = centered
initialPosition = position.positionCopy
super.init()
view.translatesAutoresizingMaskIntoConstraints = false
setupSizeConstraints()
}
public func layout() {
guard let superview = view.superview else {
return
}
NSLayoutConstraint.deactivate(constraints)
let xAttribute: NSLayoutConstraint.Attribute = centered ? .centerX : .leading
let yAttribute: NSLayoutConstraint.Attribute = centered ? .centerY : .top
let xSuperAttribute: NSLayoutConstraint.Attribute = position.left == 0 ? .leading : .trailing
let ySuperAttribute: NSLayoutConstraint.Attribute = position.top == 0 ? .top : .bottom
let xMultiplier: CGFloat = position.left == 0 ? 1 : position.left
let yMultiplier: CGFloat = position.top == 0 ? 1 : position.top
constraints = [
NSLayoutConstraint(
item: view,
attribute: xAttribute,
relatedBy: .equal,
toItem: superview,
attribute: xSuperAttribute,
multiplier: xMultiplier,
constant: 0
),
NSLayoutConstraint(
item: view,
attribute: yAttribute,
relatedBy: .equal,
toItem: superview,
attribute: ySuperAttribute,
multiplier: yMultiplier,
constant: 0
)
]
NSLayoutConstraint.activate(constraints)
view.layoutIfNeeded()
}
public func animate() {
view.superview!.layoutIfNeeded()
}
private func setupSizeConstraints() {
makeSizeConstraint(attribute: .width, constant: view.frame.width).isActive = true
makeSizeConstraint(attribute: .height, constant: view.frame.height).isActive = true
}
private func makeSizeConstraint(attribute: NSLayoutConstraint.Attribute,
constant: CGFloat) -> NSLayoutConstraint {
return NSLayoutConstraint(
item: view,
attribute: attribute,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: constant
)
}
}
public extension Content {
class func content(forTitle text: String, attributes: [NSAttributedString.Key: Any]? = nil) -> Content {
let label = UILabel(frame: CGRect.zero)
label.numberOfLines = 1
label.attributedText = NSAttributedString(string: text, attributes: attributes)
label.sizeToFit()
let position = Position(left: 0.9, bottom: 0.2)
return Content(view: label, position: position)
}
class func content(forText text: String, attributes: [NSAttributedString.Key: Any]? = nil) -> Content {
let textView = UITextView(frame: CGRect.zero)
textView.backgroundColor = UIColor.clear
textView.attributedText = NSAttributedString(string: text, attributes: attributes)
textView.sizeToFit()
return Content(view: textView, position: Position(left: 0.9, bottom: 0.1))
}
class func content(forImage image: UIImage) -> Content {
let imageView = UIImageView(image: image)
return Content(view: imageView, position: Position(left: 0.5, bottom: 0.5))
}
class func centerTransition(forSlideContent content: Content) -> Animatable {
return TransitionAnimation(
content: content,
destination: Position(left: 0.5, bottom: content.initialPosition.bottom),
duration: 2.0,
damping: 0.8,
reflective: true
)
}
}