宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//
//  UIViewExtensition.swift
//  THProject
//
//  Created by alvin_y on 2018/3/27.
//  Copyright © 2018年 yang-wang. All rights reserved.
//
import UIKit
 
public enum RGLineDirection {
    case horizontal
    case vertical
}
 
extension UIView {
    // MARK: Draw
    /// 在 view 的指定位置加指定长宽、颜色的线
    ///
    /// - Parameters:
    ///   - startPoint: 起始点坐标
    ///   - length: 长度
    ///   - width: 宽度
    ///   - color: 颜色
    ///   - direction: 方向
    /// - Returns: 按参数条件添加的线
    public func drawLine(
        from startPoint: CGPoint,
        length: CGFloat,
        width: CGFloat,
        color: UIColor,
        direction: RGLineDirection = .horizontal) -> UIView
    {
        let line = UIView()
        switch direction {
        case .horizontal:
            line.frame = CGRect(x: startPoint.x, y: startPoint.y, width: length, height: width)
            
        case .vertical:
            line.frame = CGRect(x: startPoint.x, y: startPoint.y, width: width, height: length)
        }
        line.backgroundColor = color
        self.addSubview(line)
        return line
    }
    
    /// 为视图添加边线
    ///
    /// - Parameters:
    ///   - width: 边线宽度
    ///   - cornerRadius: 边线圆角半径, 若无圆角则该值为0
    ///   - color: 边线颜色
    public func addBorder(width: CGFloat, cornerRadius: CGFloat, color: UIColor) {
        self.layer.masksToBounds = true
        self.layer.borderWidth = width
        self.layer.cornerRadius = cornerRadius
        self.layer.borderColor = color.cgColor
    }
    
    // MARK: Rect
    /// 视图尺寸
    var size: CGSize { return self.bounds.size }
    
    /// 视图横坐标最小值
    var minX: CGFloat { return self.frame.minX }
    /// 视图中心横坐标值
    var midX: CGFloat { return self.frame.midX }
    /// 视图横坐标最大值
    var maxX: CGFloat { return self.frame.maxX }
    
    /// 视图纵坐标最小值
    var minY: CGFloat { return self.frame.minY }
    /// 视图中心纵坐标值
    var midY: CGFloat { return self.frame.midY }
    /// 视图纵坐标最大值
    var maxY: CGFloat { return self.frame.maxY }
    ///  设置阴影
    func setShodView(color:UIColor? = .lightGray,shadowRadius:CGFloat=4.0,cornerRadius:CGFloat=6.0){
        if (color != nil) {
            self.layer.shadowColor =  color!.cgColor
        }else{
            self.layer.shadowColor =  #colorLiteral(red: 0.8431372549, green: 0.8431372549, blue: 0.8431372549, alpha: 1).cgColor
        }
        //设置阴影的偏移量
        self.layer.shadowOffset = CGSize(width: 0, height: 2)
        //设置阴影的透明度
        self.layer.shadowOpacity = 1
        //设置阴影的圆角
        self.layer.shadowRadius = shadowRadius
        self.layer.cornerRadius = cornerRadius
        self.clipsToBounds = false
        self.layer.masksToBounds = false
    }
  
}
 
extension UIView {
    /// 视图左上角横坐标值
    var x: CGFloat {
        get {
            return self.frame.origin.x
        }
        set {
            var frame = self.frame
            frame.origin.x = newValue
            self.frame = frame
        }
    }
    
    /// 视图左上角纵坐标值
    var y: CGFloat {
        get {
            return self.frame.origin.y
        }
        set {
            var frame = self.frame
            frame.origin.y = newValue
            self.frame = frame
        }
    }
    
    /// 视图宽度
    var width: CGFloat {
        get {
            return self.frame.size.width
        }
        set {
            var frame = self.frame
            frame.size.width = newValue
            self.frame = frame
        }
    }
    
    /// 视图高度
    var height: CGFloat {
        get {
            return self.frame.size.height
        }
        set {
            var frame = self.frame
            frame.size.height = newValue
            self.frame = frame
        }
    }
    
    /// 视图中心横坐标
    var centerX: CGFloat {
        get {
            return self.center.x
        }
        set {
            var center = self.center
            center.x = newValue
            self.center = center
        }
    }
    var right: CGFloat {
        get {
            return frame.maxX
        }
    }
    
    var bottom: CGFloat {
        get {
            return frame.maxY
        }
    }
    /// centerY
    var centerY: CGFloat {
        get {
            return center.y
        }
        
        set {
            center.y = newValue
        }
    }
}
 
// 可xib storyboard
extension UIView {
    // 边线的宽度
    @IBInspectable public var borderWidth: CGFloat {
        get {
            return self.layer.borderWidth
        }
        set {
            self.layer.borderWidth = newValue
        }
    }
    
    // 边线的颜色
    @IBInspectable public var borderColor: UIColor {
        get {
            guard let borderColor = self.layer.borderColor else {
                return UIColor.clear
            }
            return UIColor(cgColor: borderColor)
        }
        set {
            self.layer.borderColor = newValue.cgColor
        }
    }
    
    // 子图层超出部分是否裁剪
    @IBInspectable public var maskToBounds: Bool {
        get {
            return self.layer.masksToBounds
        }
        set {
            self.layer.masksToBounds = newValue
        }
    }
    
    // 圆角
    @IBInspectable public var cornerRadius: CGFloat {
        get {
            return self.layer.cornerRadius
        }
        set {
            self.layer.cornerRadius = newValue
        }
    }
    
    // 阴影颜色
    @IBInspectable public var shadowColor: UIColor {
        get {
            guard let shadowColor = self.layer.shadowColor else {
                return UIColor.clear
            }
            return UIColor(cgColor: shadowColor)
        }
        set {
            self.layer.shadowColor = newValue.cgColor
        }
    }
    
    // 阴影透明度
    @IBInspectable public var shadowOpacity: Float {
        get {
            return self.layer.shadowOpacity
        }
        set {
            self.layer.shadowOpacity = newValue
        }
    }
    
   
    
    //阴影偏移
    @IBInspectable public var shadowOffset: CGSize {
        get {
            return self.layer.shadowOffset
        }
        set {
            self.layer.shadowOffset = newValue
        }
    }
    
    // 阴影圆角
    @IBInspectable public var shadowRadius: CGFloat {
        get { 
            return self.layer.shadowRadius
        }
        set {
            self.layer.shadowRadius = newValue
        }
    }
}
 
 
 
extension UIView{
    func cutFeet(corners:UIRectCorner,radii:Int) {
        let maskPath = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii))
        let maskLayer = CAShapeLayer.init()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
    }
}
 
 
//MARK: - 地图跳动动画
extension UIView
{
    
    /// 地图跳动动画
    func popJumpAnimation()  {
        let duration = 0.3
        let height = 7
        let animation = CAKeyframeAnimation.init(keyPath: "transform.translation.y")
        let currentTy = self.transform.ty
        animation.duration = duration
        animation.values = [currentTy,currentTy - CGFloat(height / 4),currentTy - CGFloat(height / 4 * 2),currentTy - CGFloat(height / 4 * 3),currentTy - CGFloat(height),currentTy - CGFloat(height / 4 * 3),currentTy - CGFloat(height / 4 * 2),currentTy - CGFloat(height / 4),currentTy]
        animation.keyTimes = [0, 0.025, 0.085, 0.2, 0.5, 0.8, 0.915, 0.975, 1]
        animation.timingFunctions = [CAMediaTimingFunction.init(name: .easeInEaseOut)]
        animation.repeatCount = 1
        self.layer.add(animation, forKey: "kViewShakerAnimationKey")
    }
}
extension UIView {
 
    ///切部分圆角(Xib)
    func ld_cornerPartWithNib(byRoundingCorners corners: UIRectCorner, radii: CGFloat, size: CGSize) {
        let maskPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: size.width, height: size.height), byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        maskLayer.path = maskPath.cgPath
        maskLayer.shouldRasterize = true
        self.layer.mask = maskLayer
    }
    
    func addRoundedCorners(corners: UIRectCorner, rect: CGRect, radius: CGSize){
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: radius)
        let layer = CAShapeLayer()
        layer.frame = self.bounds
        layer.path = path.cgPath
        self.layer.mask = layer
    }
    
    func addRoundedCorners(corners: UIRectCorner, rect: CGRect, radius: CGSize, gradient: CAGradientLayer)
    {
        let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: rect.width, height: rect.height), byRoundingCorners: corners, cornerRadii: radius)
        let layer = CAShapeLayer()
        gradient.frame = rect
        layer.path = path.cgPath
        gradient.mask = layer
        self.layer.addSublayer(gradient)
    }
    
    func addRoundedCorners(corners: UIRectCorner, rect: CGRect, radius: CGSize, gradient: CAGradientLayer,isfill:Bool){
        if isfill {
            let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: rect.width, height: rect.height), byRoundingCorners: corners, cornerRadii: radius)
            let layer = CAShapeLayer()
            gradient.frame = rect
            layer.path = path.cgPath
            gradient.mask = layer
            self.layer.addSublayer(gradient)
        }else{
            let path = UIBezierPath(roundedRect: CGRect(x: 1, y: 1, width: rect.width - 2, height: rect.height - 2), byRoundingCorners: corners, cornerRadii: radius)
            let layer = CAShapeLayer()
            gradient.frame = rect
            layer.path = path.cgPath
            layer.fillColor = UIColor.clear.cgColor
            layer.strokeColor = UIColor.red.cgColor
            layer.lineWidth = 2
            gradient.mask = layer
            self.layer.addSublayer(gradient)
           
        }
      
    }
    
    
    
    
    func addRoundedCornersLayer(corners: UIRectCorner, rect: CGRect, radius: CGSize, fillColor: UIColor) {
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: radius)
        let layer = CAShapeLayer()
        layer.path = path.cgPath
        layer.fillColor = fillColor.cgColor
        self.layer.addSublayer(layer)
    }
    
}
//MARK: - 角标
extension UIView {
    static let buttonBadgeTag = 889
    static let badgeTag = 233
    func showBadge(badgeNum: Int, maxNum: Int = 999, percentX: Float = 0.5, percentY: Float = 0.2,color: UIColor = UIColor.red) {
        var badgeView: UIView?
        for view in self.subviews {
            if view.tag == UIView.buttonBadgeTag {
                badgeView = view
            }
        }
        if (badgeView == nil) || !(badgeView is UILabel) {
            let badgeLabel = UILabel()
            badgeLabel.tag = UIButton.buttonBadgeTag
            badgeLabel.layer.cornerRadius = 7.5
            badgeLabel.layer.masksToBounds = true
            badgeLabel.layer.zPosition = 1
            badgeLabel.backgroundColor = color
            badgeLabel.textAlignment = .center
            badgeLabel.textColor = UIColor.white
            let x: CGFloat = CGFloat(ceilf(percentX * Float(self.frame.size.width)));
            let y: CGFloat = CGFloat(ceilf(percentY * Float(self.frame.size.height)));
            
            let textWidth = "\(badgeNum)".width(12, height: 15)
            if badgeNum < 10 {
                badgeLabel.frame = CGRect(x: x, y: y, width: 15, height: 15);
            } else {
                badgeLabel.frame = CGRect(x: x, y: y, width: textWidth + 8, height: 15);
            }
            badgeLabel.adjustsFontSizeToFitWidth = true
            badgeLabel.minimumScaleFactor = 0.5
            if #available(iOS 8.2, *) {
                badgeLabel.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.medium)
            } else {
                badgeLabel.font = UIFont.systemFont(ofSize: 10)
            }
            self.addSubview(badgeLabel);
            badgeView = badgeLabel
        }
        if let badgeLabel = badgeView as? UILabel {
            badgeLabel.isHidden = (badgeNum <= 0)
            if badgeNum > maxNum {
                badgeLabel.text = "\(maxNum)+"
            } else {
                badgeLabel.text = String(badgeNum)
            }
        }
    }
    
    func showBadge(hidden: Bool, percentX: Float = 0.95, percentY: Float = 0,color: UIColor = UIColor.red,size: CGSize) {
        var badgeView: UIView?
        for view in self.subviews {
            if view.tag == UIView.badgeTag {
                badgeView = view
            }
        }
        if (badgeView == nil) {
            let badge = UIView()
            badge.tag = UIButton.badgeTag
            badge.layer.cornerRadius = size.height / 2
            badge.layer.masksToBounds = true
            badge.layer.zPosition = 1
            badge.backgroundColor = color
            let x: CGFloat = CGFloat(ceilf(percentX * Float(self.frame.size.width)));
            let y: CGFloat = CGFloat(ceilf(percentY * Float(self.frame.size.height)));
            badge.frame = CGRect(x: x, y: y, width: size.width, height: size.height);
            self.addSubview(badge);
            badgeView = badge
        }
        if let badge = badgeView {
            badge.isHidden = hidden
        }
    }
}
 
//MARK: - 设置试图阴影
extension UIView{
    //设置试图阴影
    func configShadow(opacity: Float = 0.1, color: UIColor = .black, radius: CGFloat = 5.0, offset: CGSize = CGSize(width: 1, height: 1)){
        self.clipsToBounds = false
        self.backgroundColor = .clear
        self.layer.shadowOffset = offset
        self.layer.shadowColor = color.cgColor
        self.layer.shadowRadius = radius
        self.layer.shadowOpacity = opacity
    }
    func configShadow1(opacity: Float = 0.1, color: UIColor = .black, radius: CGFloat = 5.0, offset: CGSize = CGSize(width: 1, height: 1)){
        self.clipsToBounds = false
        self.layer.shadowOffset = offset
        self.layer.shadowColor = color.cgColor
        self.layer.shadowRadius = radius
        self.layer.shadowOpacity = opacity
    }
}