22
22
#if canImport(UIKit) && (os(iOS) || os(tvOS))
23
23
import UIKit
24
24
25
- public extension UIView {
25
+ extension UIView {
26
26
27
27
/**
28
28
SparrowKit: Init `UIView` object with background color.
29
29
30
30
- parameter backgroundColor: Color which using for background.
31
31
*/
32
- convenience init ( backgroundColor color: UIColor ) {
32
+ public convenience init ( backgroundColor color: UIColor ) {
33
33
self . init ( )
34
34
backgroundColor = color
35
35
}
@@ -42,7 +42,7 @@ public extension UIView {
42
42
- warning:
43
43
If view not added to any controller, return nil.
44
44
*/
45
- var viewController : UIViewController ? {
45
+ open var viewController : UIViewController ? {
46
46
weak var parentResponder : UIResponder ? = self
47
47
while parentResponder != nil {
48
48
parentResponder = parentResponder!. next
@@ -58,21 +58,21 @@ public extension UIView {
58
58
59
59
- parameter subviews: Array of `UIView` objects.
60
60
*/
61
- func addSubviews( _ subviews: [ UIView ] ) {
61
+ open func addSubviews( _ subviews: [ UIView ] ) {
62
62
subviews. forEach { addSubview ( $0) }
63
63
}
64
64
65
65
/**
66
66
SparrowKit: Remove all subviews.
67
67
*/
68
- func removeSubviews( ) {
68
+ open func removeSubviews( ) {
69
69
subviews. forEach { $0. removeFromSuperview ( ) }
70
70
}
71
71
72
72
/**
73
73
SparrowKit: Take screenshoot of view as `UIImage`.
74
74
*/
75
- var screenshot : UIImage ? {
75
+ open var screenshot : UIImage ? {
76
76
UIGraphicsBeginImageContextWithOptions ( layer. frame. size, false , 0 )
77
77
defer {
78
78
UIGraphicsEndImageContext ( )
@@ -82,6 +82,16 @@ public extension UIView {
82
82
return UIGraphicsGetImageFromCurrentImageContext ( )
83
83
}
84
84
85
+ /**
86
+ SparrowKit: If view has LTR interface.
87
+ */
88
+ open var ltr : Bool { effectiveUserInterfaceLayoutDirection == . leftToRight }
89
+
90
+ /**
91
+ SparrowKit: If view has TRL interface.
92
+ */
93
+ open var rtl : Bool { effectiveUserInterfaceLayoutDirection == . rightToLeft }
94
+
85
95
// MARK: - Layout
86
96
87
97
/**
@@ -90,7 +100,7 @@ public extension UIView {
90
100
- warning:
91
101
Fit view can be return zero height. View shoud support it.
92
102
*/
93
- func setWidthAndFit( width: CGFloat ) {
103
+ open func setWidthAndFit( width: CGFloat ) {
94
104
frame. setWidth ( width)
95
105
sizeToFit ( )
96
106
}
@@ -101,7 +111,7 @@ public extension UIView {
101
111
- warning:
102
112
If current view have not superview, center X is set to zero.
103
113
*/
104
- func setXCenter( ) {
114
+ open func setXCenter( ) {
105
115
center. x = ( superview? . frame. width ?? 0 ) / 2
106
116
}
107
117
@@ -111,7 +121,7 @@ public extension UIView {
111
121
- warning:
112
122
If current view have not superview, center Y is set to zero.
113
123
*/
114
- func setYCenter( ) {
124
+ open func setYCenter( ) {
115
125
center. y = ( superview? . frame. height ?? 0 ) / 2
116
126
}
117
127
@@ -121,7 +131,7 @@ public extension UIView {
121
131
- warning:
122
132
If current view have not superview, center is set to zero.
123
133
*/
124
- func setToCenter( ) {
134
+ open func setToCenter( ) {
125
135
setXCenter ( )
126
136
setYCenter ( )
127
137
}
@@ -131,7 +141,7 @@ public extension UIView {
131
141
/**
132
142
SparrowKit: Margins of readable frame.
133
143
*/
134
- var readableMargins : UIEdgeInsets {
144
+ open var readableMargins : UIEdgeInsets {
135
145
let layoutFrame = readableContentGuide. layoutFrame
136
146
return UIEdgeInsets (
137
147
top: layoutFrame. origin. y,
@@ -144,21 +154,21 @@ public extension UIView {
144
154
/**
145
155
SparrowKit: Readable width of current view without horizontal readable margins.
146
156
*/
147
- var readableWidth : CGFloat {
157
+ open var readableWidth : CGFloat {
148
158
return readableContentGuide. layoutFrame. width
149
159
}
150
160
151
161
/**
152
162
SparrowKit: Readable height of current view without vertical readable margins.
153
163
*/
154
- var readableHeight : CGFloat {
164
+ open var readableHeight : CGFloat {
155
165
return readableContentGuide. layoutFrame. height
156
166
}
157
167
158
168
/**
159
169
SparrowKit: Readable frame of current view without vertical and horizontal readable margins.
160
170
*/
161
- var readableFrame : CGRect {
171
+ open var readableFrame : CGRect {
162
172
let margins = readableMargins
163
173
return CGRect . init ( x: margins. left, y: margins. top, width: readableWidth, height: readableHeight)
164
174
}
@@ -168,7 +178,7 @@ public extension UIView {
168
178
/**
169
179
SparrowKit: Width of current view without horizontal layout margins.
170
180
*/
171
- var layoutWidth : CGFloat {
181
+ open var layoutWidth : CGFloat {
172
182
// ver 1
173
183
// Depricated becouse sometimes return invalid size
174
184
//return layoutMarginsGuide.layoutFrame.width
@@ -180,7 +190,7 @@ public extension UIView {
180
190
/**
181
191
SparrowKit: Height of current view without vertical layout margins.
182
192
*/
183
- var layoutHeight : CGFloat {
193
+ open var layoutHeight : CGFloat {
184
194
// ver 1
185
195
// Depricated becouse sometimes return invalid size
186
196
//return layoutMarginsGuide.layoutFrame.height
@@ -192,7 +202,7 @@ public extension UIView {
192
202
/**
193
203
SparrowKit: Frame of current view without horizontal and vertical layout margins.
194
204
*/
195
- var layoutFrame : CGRect {
205
+ open var layoutFrame : CGRect {
196
206
return CGRect . init ( x: layoutMargins. left, y: layoutMargins. top, width: layoutWidth, height: layoutHeight)
197
207
}
198
208
@@ -202,15 +212,15 @@ public extension UIView {
202
212
- warning:
203
213
If view not have superview, nothing happen.
204
214
*/
205
- func setEqualSuperviewBounds( ) {
215
+ open func setEqualSuperviewBounds( ) {
206
216
guard let superview = self . superview else { return }
207
217
frame = superview. bounds
208
218
}
209
219
210
220
/**
211
221
SparrowKit: Set view equal frame to superview frame via `autoresizingMask`.
212
222
*/
213
- func setEqualSuperviewBoundsWithAutoresizingMask( ) {
223
+ open func setEqualSuperviewBoundsWithAutoresizingMask( ) {
214
224
autoresizingMask = [ . flexibleWidth, . flexibleHeight]
215
225
}
216
226
@@ -220,7 +230,7 @@ public extension UIView {
220
230
- warning:
221
231
If view not have superview, constraints will not be added.
222
232
*/
223
- func setEqualSuperviewBoundsWithAutoLayout( ) {
233
+ open func setEqualSuperviewBoundsWithAutoLayout( ) {
224
234
guard let superview = self . superview else { return }
225
235
translatesAutoresizingMaskIntoConstraints = false
226
236
NSLayoutConstraint . activate ( [
@@ -237,7 +247,7 @@ public extension UIView {
237
247
- warning:
238
248
If view not have superview, constraints will not be added.
239
249
*/
240
- func setEqualSuperviewMarginsWithAutoLayout( ) {
250
+ open func setEqualSuperviewMarginsWithAutoLayout( ) {
241
251
guard let superview = self . superview else { return }
242
252
translatesAutoresizingMaskIntoConstraints = false
243
253
NSLayoutConstraint . activate ( [
@@ -253,7 +263,7 @@ public extension UIView {
253
263
/**
254
264
SparrowKit: Wrapper for layer property `masksToBounds`.
255
265
*/
256
- var masksToBounds : Bool {
266
+ open var masksToBounds : Bool {
257
267
get {
258
268
return layer. masksToBounds
259
269
}
@@ -271,7 +281,7 @@ public extension UIView {
271
281
- parameter corners: Case of `UIRectCorner`
272
282
- parameter radius: Amount of radius.
273
283
*/
274
- func roundCorners( _ corners: UIRectCorner = . allCorners, radius: CGFloat ) {
284
+ open func roundCorners( _ corners: UIRectCorner = . allCorners, radius: CGFloat ) {
275
285
let maskPath = UIBezierPath (
276
286
roundedRect: bounds,
277
287
byRoundingCorners: corners,
@@ -288,14 +298,14 @@ public extension UIView {
288
298
- important:
289
299
Need call after changed frame. Better leave it in `layoutSubviews` method.
290
300
*/
291
- func roundCorners( ) {
301
+ open func roundCorners( ) {
292
302
layer. cornerRadius = min ( frame. width, frame. height) / 2
293
303
}
294
304
295
305
/**
296
306
SparrowKit: Wrapper for layer property `borderColor`.
297
307
*/
298
- var borderColor : UIColor ? {
308
+ open var borderColor : UIColor ? {
299
309
get {
300
310
guard let color = layer. borderColor else { return nil }
301
311
return UIColor ( cgColor: color)
@@ -314,7 +324,7 @@ public extension UIView {
314
324
/**
315
325
SparrowKit: Wrapper for layer property `borderWidth`.
316
326
*/
317
- var borderWidth : CGFloat {
327
+ open var borderWidth : CGFloat {
318
328
get {
319
329
return layer. borderWidth
320
330
}
@@ -331,7 +341,7 @@ public extension UIView {
331
341
- parameter offset: Vertical and horizontal offset from center fro shadow.
332
342
- parameter opacity: Alpha for shadow view.
333
343
*/
334
- func addShadow( ofColor color: UIColor , radius: CGFloat , offset: CGSize , opacity: Float ) {
344
+ open func addShadow( ofColor color: UIColor , radius: CGFloat , offset: CGSize , opacity: Float ) {
335
345
layer. shadowColor = color. cgColor
336
346
layer. shadowOffset = offset
337
347
layer. shadowRadius = radius
@@ -345,7 +355,7 @@ public extension UIView {
345
355
346
356
- parameter amount: Amount of paralax effect.
347
357
*/
348
- func addParalax( amount: CGFloat ) {
358
+ open func addParalax( amount: CGFloat ) {
349
359
motionEffects. removeAll ( )
350
360
let horizontal = UIInterpolatingMotionEffect ( keyPath: " center.x " , type: . tiltAlongHorizontalAxis)
351
361
horizontal. minimumRelativeValue = - amount
@@ -363,7 +373,7 @@ public extension UIView {
363
373
/**
364
374
SparrowKit: Remove paralax.
365
375
*/
366
- func removeParalax( ) {
376
+ open func removeParalax( ) {
367
377
motionEffects. removeAll ( )
368
378
}
369
379
@@ -375,8 +385,8 @@ public extension UIView {
375
385
- parameter duration: Duration of animation.
376
386
- parameter completion: Completion when animation ended.
377
387
*/
378
- func fadeIn( duration: TimeInterval = 0.3 , completion: ( ( Bool ) -> Void ) ? = nil ) {
379
- UIView . animate ( withDuration: duration, animations: {
388
+ open func fadeIn( duration: TimeInterval = 0.3 , completion: ( ( Bool ) -> Void ) ? = nil ) {
389
+ UIView . animate ( withDuration: duration, delay : . zero , options : [ . beginFromCurrentState , . allowUserInteraction ] , animations: {
380
390
self . alpha = 1
381
391
} , completion: completion)
382
392
}
@@ -387,8 +397,8 @@ public extension UIView {
387
397
- parameter duration: Duration of animation.
388
398
- parameter completion: Completion when animation ended.
389
399
*/
390
- func fadeOut( duration: TimeInterval = 0.3 , completion: ( ( Bool ) -> Void ) ? = nil ) {
391
- UIView . animate ( withDuration: duration, animations: {
400
+ open func fadeOut( duration: TimeInterval = 0.3 , completion: ( ( Bool ) -> Void ) ? = nil ) {
401
+ UIView . animate ( withDuration: duration, delay : . zero , options : [ . beginFromCurrentState , . allowUserInteraction ] , animations: {
392
402
self . alpha = 0
393
403
} , completion: completion)
394
404
}
0 commit comments