美文网首页
swift自定义直方图

swift自定义直方图

作者: zhangml0522 | 来源:发表于2019-06-20 10:23 被阅读0次

先放预览结果图:


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)
        
    }

相关文章

  • swift自定义直方图

    先放预览结果图: 以及swift文件: 接下来直接贴代码加注释:HistogramView.swift 使用方式

  • Chapter3 Decorate Graphs with Pl

    本章探索 *线样式自定义 *点样式自定义 *轴刻度自定义 *Matplotlib中可用的多种图类型,比如直方图(h...

  • iOS - Charts 直方图

    关键字:iOS Charts 图表 swift BarChart 直方图 本文给出两个使用 danielgindi...

  • Swift

    1.Swift构建自定义控件2.Swift开发集锦3.Swift构建自定义控件4.Cocoa的Swift开发专题5...

  • 分析实现Android自定义View之递增直方图

    继上次分析实现Android自定义View之扇形图之后,自己又画了下面的这个递增直方图,本来是想做个静态的直方图就...

  • Android自定义柱状图表

    本文通过示例代码介绍如何自定义简单的直方图表,此图表并非常见的直方图表,而是可以分组的。此文不会过多涉及原理,比较...

  • swift基础1 -- 输出

    自定义打印可参考swift自定义打印

  • Swift中自定义Log

    Swift中自定义Log Swift中自定义Log:依次是类名.方法名.行号.内容. func LTLog(fun...

  • UITextField的一些笔记

    暴走萝莉 ViewController.swift YTPersonImageView.swift 2.自定义键盘...

  • iOS 自定义Log

    swift自定义log一般写在AppDelegate里:AppDelegate.swift:application...

网友评论

      本文标题:swift自定义直方图

      本文链接:https://www.haomeiwen.com/subject/aztdqctx.html