Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class HistogramView: UIView {
- var barWidth: CGFloat = 3.0
- var barMinHeight: CGFloat = 3.0
- var marginBetweenBars: CGFloat = 2.0
- var bars = [UIView]()
- override init(frame: CGRect) {
- super.init(frame: frame)
- backgroundColor = UIColor.clear
- for i in 0...15 {
- var x = CGFloat(i) * barWidth
- if i > 0 {
- x += CGFloat(i) * marginBetweenBars
- }
- let y = (self.bounds.height - barMinHeight)
- let barRect = CGRect(x: x, y: y,
- width: barWidth, height: barMinHeight)
- let v = UIView(frame: barRect)
- v.backgroundColor = UIColor.white
- v.layer.cornerRadius = barWidth / 2
- v.layer.shadowOpacity = 0.8
- v.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
- v.layer.shadowColor = UIColor.gray.cgColor
- v.layer.shadowRadius = 1.3
- bars.append(v)
- self.addSubview(v)
- }
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- func didUpdateHistogramRaw(data: [Int]) {
- let max_pixels = CGFloat(data.max()!)
- let height = CGFloat(self.bounds.height)
- for (i, item) in data.enumerated() {
- var value_height = CGFloat(item) / max_pixels * height
- if value_height < barMinHeight {
- value_height = barMinHeight
- }
- let y = (self.bounds.height - value_height)
- let v = self.bars[i]
- UIView.animate(withDuration: 0.2, animations: {
- v.frame.origin.y = y
- v.frame.size.height = value_height
- })
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement