說明:
自己寫了一個(gè)餅狀圖的控件解藻,可以旋轉(zhuǎn)(如果嫌棄旋轉(zhuǎn)范圍太窄媒鼓,可自行調(diào)節(jié))辽狈,可以出發(fā)點(diǎn)擊事件勋又。
注意:
如果視圖放在scrollView鸠蚪,或者有特定手勢存在的視圖上的時(shí)候孙咪;會(huì)出現(xiàn)旋轉(zhuǎn)不暢的現(xiàn)象宁改,如果能
解決,自行解決愉棱;解決不了可以放棄旋轉(zhuǎn)功能唆铐,或者放棄使用該控件。
ps:我是遇到了放在一個(gè)第三方的控件上羽氮,無論我怎么調(diào)整都被它影響或链,所以在公司app中,我放棄了
旋轉(zhuǎn)功能
代碼:
/**********************************/
/********* 餅狀圖 **********/
/**********************************/
class XJJPCItem {
var title: String?
var description: String?
var id: Int = 0
var scale: CGFloat = 0
var lineWidth: CGFloat = 1
var lineColor: UIColor?
var strockColor: UIColor?
var strockWidth: CGFloat = 1
var fillColor: UIColor?
//記錄值档押,不用傳入
var startAngle: CGFloat?
var endAngle: CGFloat?
var isSelected: Bool = false
init(_ title: String?,
description: String?,
id: Int?,
scale: CGFloat?,
lineWidth: CGFloat?,
lineColor: UIColor?,
strockColor: UIColor?,
strockWidth: CGFloat?,
fillColor: UIColor?) {
self.title = title
self.description = description
self.id = id ?? 0
self.scale = scale ?? 0
self.lineWidth = lineWidth ?? 1
self.lineColor = lineColor
self.strockColor = strockColor
self.strockWidth = strockWidth ?? 1
self.fillColor = fillColor
}
}
class XJJPCView: UIView {
var touchUpBlock: ((_ id: Int) -> Void)?
var dataSource: [XJJPCItem]? {
didSet {
guard let data = dataSource else {return}
self.source = data
self.setNeedsDisplay()
}
}
var defaultAngle: CGFloat = -1 / 4//以pi_2為基礎(chǔ)設(shè)置,-1/4即為:-pi/2
var offset: CGFloat?//位移量祈纯,用于手勢滑動(dòng)
//文字屬性
var textFont: UIFont!
var textColor: UIColor!
override init(frame: CGRect) {
super.init(frame: frame)
self.initUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touche: UITouch in touches {
let point: CGPoint = touche.location(in: self)
self.tap(point)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touche: UITouch in touches {
let point: CGPoint = touche.location(in: self)
let previous: CGPoint = touche.previousLocation(in: self)
if (point.x - previous.x) > 1 || (point.y - previous.y) > 1 {
self.move(point, previous)
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
}
//移動(dòng)
private func move(_ point: CGPoint, _ previous: CGPoint) {
let center: CGPoint = CGPoint(x: self.bounds.width / 2, y: self.bounds.height / 2)
let x: CGFloat = point.x - center.x
let y: CGFloat = point.y - previous.y
let per_angle: CGFloat = pi_2 / 100
if x > 0 {
self.diff_angle += per_angle * (y / 10)
}else {
self.diff_angle += -per_angle * (y / 10)
}
self.setNeedsDisplay()
}
//點(diǎn)擊
private func tap(_ point: CGPoint) {
let center: CGPoint = CGPoint(x: self.bounds.width / 2, y: self.bounds.height / 2)
let x: Double = Double(point.x - center.x)
let y: Double = Double(point.y - center.y)
let lenght: Double = sqrt(x * x + y * y)
let angle: CGFloat = CGFloat(acos(x / lenght))
let d_angle: CGFloat = (y >= 0) ? angle : -angle
let a = self.angleTranslate(d_angle)
if let r = self.sectorRadius {
let _r = Double(r)
if x > _r || x < -_r || y > _r || y < -_r {
return
}
}
self.source.forEach {[weak self] (item) in
guard let sself = self else {return}
item.isSelected = false
if let b = item.startAngle, let c = item.endAngle {
if b > c {
if (a > b && a <= pi_2) || (a < c && a > 0) {
sself.touchUpBlock?(item.id)
item.isSelected = true
}
}else {
if a > b, a < c {
sself.touchUpBlock?(item.id)
item.isSelected = true
}
}
}
sself.setNeedsDisplay()
}
}
private let pi_2 = CGFloat(Double.pi * 2)
private var sectorRadius: CGFloat?//扇形區(qū)域半徑
private var diff_angle: CGFloat = 0//旋轉(zhuǎn)時(shí)的變化量
private var source: [XJJPCItem] = []
private func initUI() {
self.backgroundColor = UIColor.clear
self.textFont = UIFont.boldSystemFont(ofSize: 13)
self.textColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
}
//將角度轉(zhuǎn)化為0~pi_2之間的值
private func angleTranslate(_ angle: CGFloat) -> CGFloat {
if angle < 0 {
let multiple: Int = Int(-angle / pi_2) + 1
return angle + pi_2 * CGFloat(multiple)
}else if angle > pi_2 {
let multiple = Int(angle / pi_2)
return angle - pi_2 * CGFloat(multiple)
}
return angle
}
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let current = UIGraphicsGetCurrentContext() else {return}
current.clear(rect)
current.setFillColor(UIColor.clear.cgColor)
current.fill(rect)
self.drawOnContext(current)
}
//根據(jù)數(shù)據(jù)畫出圖形
private func drawOnContext(_ context: CGContext) {
guard source.count > 0 else {return}
let center: CGPoint = CGPoint(x: self.bounds.width / 2, y: self.bounds.height / 2)
let r: CGFloat = self.bounds.height / 5 * 3 / 2
var start: CGFloat = 0
var end: CGFloat = 0
self.sectorRadius = r
var currentAngle: CGFloat = defaultAngle * pi_2 + diff_angle
for item in source {
start = currentAngle
end = start + item.scale * pi_2
let text: String = (item.title ?? "") + ": \(item.scale)"
self.creatSector(context, center, r, start, end, item.strockWidth, item.strockColor ?? #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1), item.fillColor ?? #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1))
let result = self.creatLine(context, center, r, start, item.scale * pi_2, item.lineColor ?? #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1), item.lineWidth)
self.creatText(context, text, result.0, result.1)
if item.isSelected == true {
self.creatSelectSector(context, center, r + 5, start, end, 10, item.strockColor ?? #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1), item.fillColor ?? #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1))
}
currentAngle = end
item.startAngle = self.angleTranslate(start)
item.endAngle = self.angleTranslate(end)
}
}
//畫扇形
private func creatSector(_ context: CGContext,
_ center: CGPoint,
_ radius: CGFloat,
_ startAngle: CGFloat,
_ endAngle: CGFloat,
_ strockWidth: CGFloat,
_ strockColor: UIColor,
_ fillColor: UIColor) {
context.move(to: center)
context.addArc(center: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: false)
context.setStrokeColor(strockColor.cgColor)
context.setFillColor(fillColor.cgColor)
context.setLineWidth(strockWidth)
context.closePath()
context.drawPath(using: .fillStroke)
}
//選中狀態(tài)圓弧
private func creatSelectSector(_ context: CGContext,
_ center: CGPoint,
_ radius: CGFloat,
_ startAngle: CGFloat,
_ endAngle: CGFloat,
_ strockWidth: CGFloat,
_ strockColor: UIColor,
_ fillColor: UIColor) {
let s_color: UIColor = fillColor.withAlphaComponent(0.4)
context.addArc(center: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: false)
context.setStrokeColor(s_color.cgColor)
context.setLineWidth(strockWidth)
context.drawPath(using: .stroke)
}
//找到圓弧上直線經(jīng)過的點(diǎn)令宿,并返回圓心到這一點(diǎn)所在直線的任意一點(diǎn),通過scale實(shí)現(xiàn)
private func findPoint(_ center: CGPoint,
_ radius: CGFloat,
_ startAngle: CGFloat,
_ moveAngle: CGFloat,
_ scale: CGFloat) -> CGPoint {
let angle: CGFloat = startAngle + moveAngle / 2
let o_x: CGFloat = radius * cos(angle)
let o_y: CGFloat = radius * sin(angle)
let c_x: CGFloat = center.x + o_x * scale
let c_y: CGFloat = center.y + o_y * scale
return CGPoint(x: c_x, y: c_y)
}
//畫線
private func creatLine(_ context: CGContext,
_ center: CGPoint,
_ radius: CGFloat,
_ startAngle: CGFloat,
_ moveAngle: CGFloat,
_ lineColor: UIColor,
_ lineWidth: CGFloat) -> (CGPoint, Bool) {
let centerAngle: CGFloat = self.angleTranslate(startAngle + moveAngle / 2)
let isLeft: Bool = (centerAngle > pi_2 / 4) && (centerAngle < pi_2 / 4 * 3)
let start: CGPoint = self.findPoint(center, radius, startAngle, moveAngle, 0.8)
let middle: CGPoint = self.findPoint(center, radius, startAngle, moveAngle, 1.2)
let diff: CGFloat = 1 - fabs(middle.x - center.x) / (radius * 1.2)
let end: CGPoint = CGPoint(x: middle.x + (isLeft ? -20 : 20) * diff, y: middle.y)
context.move(to: start)
context.addLine(to: middle)
context.addLine(to: end)
context.setLineWidth(lineWidth)
context.setStrokeColor(lineColor.cgColor)
context.drawPath(using: .stroke)
return (end, isLeft)
}
//寫文字
private func creatText(_ context: CGContext, _ text: String, _ position: CGPoint, _ isLeft: Bool) {
let style = NSMutableParagraphStyle()
style.lineBreakMode = .byWordWrapping
style.alignment = .center
/*之前的寫法
let dic: [String : Any] = [
NSFontAttributeName: textFont,
NSForegroundColorAttributeName: textColor,
NSParagraphStyleAttributeName: style,
]
*/
let dic: [NSAttributedStringKey : Any] = [
.font: textFont,
.foregroundColor: textColor,
.paragraphStyle: style,
]
let size = text.size(withAttributes: dic)
let x: CGFloat = isLeft ? (position.x - size.width) : position.x
let y: CGFloat = position.y - size.height / 2
let rect = CGRect(x: x, y: y, width: size.width, height: size.height)
text.draw(in: rect, withAttributes: dic)
}
}