先放预览结果图:
image.gif
以及swift文件:
xcode
接下来直接贴代码加注释:HistogramView.swift
import UIKit
class HistogramView: UIView {
init(frame: CGRect, arrayValue:[CGFloat]) {
super.init(frame: frame)
//创建直方图的方法, arrayValue为创建直方图时传进来的数组,就是直方图应该显示的值
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()
}
//创建直方图
func createHistogram(arrayValue:[CGFloat]){
//取到数组的最大值最小值
let value = minMax(arr: arrayValue)
//设置直方图最大高度为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
//根据显示内容的多少来设置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)
//创建每一个直方图
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")
//高度的动画效果,不设置这个位移动画的话高度动画会从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")
//设置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)
}
//获取数组最大值和最小值
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)
}
网友评论