先放預(yù)覽結(jié)果圖:
image.gif
以及swift文件:
xcode
接下來直接貼代碼加注釋:HistogramView.swift
import UIKit
class HistogramView: UIView {
init(frame: CGRect, arrayValue:[CGFloat]) {
super.init(frame: frame)
//創(chuàng)建直方圖的方法, arrayValue為創(chuàng)建直方圖時傳進(jìn)來的數(shù)組婿着,就是直方圖應(yīng)該顯示的值
createHistogram(arrayValue: arrayValue)
}
//畫底部的線蝴蜓,左右間距10搀菩,下部間距9
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else {
return
}
let drawingRect = bounds
let path = CGMutablePath()
path.move(to: CGPoint(x: 10, y: drawingRect.height-9))
path.addLine(to: CGPoint(x: drawingRect.width-10, y: drawingRect.height-9))
context.setStrokeColor(UIColor.black.cgColor)
context.addPath(path)
context.strokePath()
}
//創(chuàng)建直方圖
func createHistogram(arrayValue:[CGFloat]){
//取到數(shù)組的最大值最小值
let value = minMax(arr: arrayValue)
//設(shè)置直方圖最大高度為bounds.height - 40严沥,距底部10熟嫩,距頂部30片习,用這個高度除以最大值來計算每一個值的單位高度
let unitHeight = (bounds.height - 40) / CGFloat(value.1)
let scrollView = UIScrollView(frame: CGRect(x: 10, y: 0, width: bounds.width-20, height: bounds.height))
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
//根據(jù)顯示內(nèi)容的多少來設(shè)置scrollView的contentSize
let ContentSizeWidth = (40 * CGFloat(arrayValue.count) + 20) > scrollView.bounds.width ? (40 * CGFloat(arrayValue.count) + 20) : scrollView.bounds.width
scrollView.contentSize = CGSize(width: ContentSizeWidth, height: bounds.height-10)
//創(chuàng)建每一個直方圖
for i in 0 ..< arrayValue.count {
//高度為實際值*單位高度
let height = arrayValue[i] * unitHeight
//寬度為40照卦,左右間距為10
let x = 40.0*CGFloat(i)+10
//底部間距為10
let y = bounds.height - 10 - height
let rectView = UIView(frame: CGRect(x: x, y: y, width: 30.0, height: height))
rectView.backgroundColor = UIColor.blue
scrollView.addSubview(rectView)
//高度的動畫效果
let rotationAnim = CABasicAnimation(keyPath: "bounds")
rotationAnim.repeatCount = 1
rotationAnim.duration = 1
rotationAnim.fromValue = NSValue(cgRect: CGRect(x: 0, y: 0, width: rectView.bounds.width, height: 0))
rotationAnim.toValue = NSValue(cgRect: rectView.bounds)
rectView.layer.add(rotationAnim, forKey: "bounds")
//高度的動畫效果,不設(shè)置這個位移動畫的話高度動畫會從y的中點開始而不是從底部開始
let rotationAnim2 = CABasicAnimation(keyPath: "position")
rotationAnim2.repeatCount = 1
rotationAnim2.duration = 1
rotationAnim2.fromValue = NSValue(cgPoint: CGPoint(x: rectView.center.x, y: bounds.height - 10))
rotationAnim2.toValue = NSValue(cgPoint: rectView.center)
rectView.layer.add(rotationAnim2, forKey: "position")
//設(shè)置UILabel來顯示具體值
let text = UILabel(frame: CGRect(x: x, y: y-15, width: 30.0, height: 14.0))
text.text = "\(NSInteger(arrayValue[i]))"
text.font = UIFont.systemFont(ofSize: 12)
text.textAlignment = .center
scrollView.addSubview(text)
//UILabel的位移動畫狸驳,和直方圖一起上升
let rotationAnim3 = CABasicAnimation(keyPath: "position")
rotationAnim3.repeatCount = 1
rotationAnim3.duration = 1
rotationAnim3.isRemovedOnCompletion = false
rotationAnim3.fromValue = NSValue(cgPoint: CGPoint(x: text.center.x, y: bounds.height - 17))
rotationAnim3.toValue = NSValue(cgPoint: text.center)
text.layer.add(rotationAnim3, forKey: "position")
}
self.addSubview(scrollView)
}
//獲取數(shù)組最大值和最小值
func minMax(arr:[CGFloat])->(CGFloat,CGFloat) {
var min:CGFloat = arr[0]
var max:CGFloat = arr[0]
for i in 0..<arr.count {
if arr[i] < min {
min = arr[i]
}
if arr[i] > max {
max = arr[i]
}
}
return (min,max)
}
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
使用方式
override func viewDidLoad() {
super.viewDidLoad()
let array : [CGFloat] = [100,200,150,75,20,50,60,130,110,190,40,55,23,99]
self.view.backgroundColor = UIColor.white
let view = HistogramView(frame: CGRect(x: 10, y: 100, width: self.view.frame.width-20, height: 200), arrayValue: array)
view.backgroundColor = UIColor.green
view.tag = 1001
self.view.addSubview(view)
}