CNLiveRoundedRectView.swift
3.48 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
//
// CNLiveRoundedRectView.swift
// metaCode
//
// Created by zx on 2023/11/14.
//
import Foundation
import CoreGraphics
let DS_RRV_ONE_PIXEL = 1.0 / UIScreen.main.scale
class CNLiveRoundedRectView: UIView {
//填充色
var _fillColor:UIColor?
var fillColor:UIColor?{
get{
_fillColor
}
set{
_fillColor = newValue
self.setNeedsDisplay()
}
}
//边框色
var _strokeColor:UIColor?
var strokeColor:UIColor?{
get{
_strokeColor
}
set{
_strokeColor = newValue
self.setNeedsDisplay()
}
}
//边框宽度
var _strokeWidth:CGFloat = 0.0
var strokeWidth:CGFloat{
get{
_strokeWidth
}
set{
_strokeWidth = newValue
self.setNeedsDisplay()
}
}
//圆角度数 默认0
var _radius:CGFloat = 0.0
var radius:CGFloat{
get{
_radius
}
set{
_radius = newValue
self.setNeedsDisplay()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .clear
}
override var frame:CGRect{
didSet {
let newFrame = frame
super.frame = newFrame
self.setNeedsDisplay()
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
super.draw(rect)
if ((self.strokeColor != nil && self.strokeColor?.cgColor != UIColor.clear.cgColor) && ( self.fillColor != nil && self.fillColor?.cgColor != UIColor.clear.cgColor))
{
let fpath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.radius)
self.fillColor?.setFill()
fpath.fill()
let lineWidth = self.strokeWidth == 0 ? DS_RRV_ONE_PIXEL : self.strokeWidth
let fx = lineWidth
let fy = lineWidth
let fwidth = self.width - fx*2
let fheight = self.height - fy*2
let spath = UIBezierPath(roundedRect: CGRect(x: fx, y: fy, width: fwidth, height: fheight), cornerRadius: self.radius-lineWidth)
spath.lineWidth = lineWidth
self.strokeColor?.setStroke()
spath.stroke()
}else if ((self.strokeColor != nil && self.strokeColor?.cgColor != UIColor.clear.cgColor) && (self.fillColor == nil || self.fillColor?.cgColor == UIColor.clear.cgColor)) {
UIColor.clear.setFill()
let lineWidth = self.strokeWidth == 0 ? DS_RRV_ONE_PIXEL : self.strokeWidth
let x = lineWidth
let y = lineWidth
let width = self.width - x*2
let height = self.height - y*2
let path = UIBezierPath(roundedRect: CGRect(x: x, y: y, width: width, height: height), cornerRadius: self.radius-lineWidth)
path.lineWidth = lineWidth
self.strokeColor?.setStroke()
path.stroke()
}else if ((self.strokeColor == nil || self.strokeColor?.cgColor == UIColor.clear.cgColor) && (self.fillColor != nil && self.fillColor?.cgColor != UIColor.clear.cgColor)){
UIColor.clear.setStroke()
let path = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.radius)
self.fillColor?.setFill()
path.fill()
}else{
UIColor.clear.setStroke()
UIColor.clear.setFill()
}
}
}