Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.95 KB | None | 0 0
  1. import UIKit
  2.  
  3. class HistogramView: UIView {
  4.     var barWidth: CGFloat = 3.0
  5.     var barMinHeight: CGFloat = 3.0
  6.     var marginBetweenBars: CGFloat = 2.0
  7.    
  8.     var bars = [UIView]()
  9.    
  10.     override init(frame: CGRect) {
  11.         super.init(frame: frame)
  12.        
  13.         backgroundColor = UIColor.clear
  14.        
  15.         for i in 0...15 {
  16.             var x = CGFloat(i) * barWidth
  17.            
  18.             if i > 0 {
  19.                 x += CGFloat(i) * marginBetweenBars
  20.             }
  21.            
  22.             let y = (self.bounds.height - barMinHeight)
  23.            
  24.             let barRect = CGRect(x: x, y: y,
  25.                                  width: barWidth, height: barMinHeight)
  26.            
  27.             let v = UIView(frame: barRect)
  28.             v.backgroundColor = UIColor.white
  29.             v.layer.cornerRadius = barWidth / 2
  30.            
  31.             v.layer.shadowOpacity = 0.8
  32.             v.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
  33.             v.layer.shadowColor = UIColor.gray.cgColor
  34.             v.layer.shadowRadius = 1.3
  35.            
  36.             bars.append(v)
  37.             self.addSubview(v)
  38.         }
  39.     }
  40.    
  41.     required init?(coder aDecoder: NSCoder) {
  42.         fatalError("init(coder:) has not been implemented")
  43.     }
  44.    
  45.     func didUpdateHistogramRaw(data: [Int]) {
  46.         let max_pixels = CGFloat(data.max()!)
  47.         let height = CGFloat(self.bounds.height)
  48.        
  49.         for (i, item) in data.enumerated() {
  50.             var value_height = CGFloat(item) / max_pixels * height
  51.            
  52.             if value_height < barMinHeight {
  53.                 value_height = barMinHeight
  54.             }
  55.            
  56.             let y = (self.bounds.height - value_height)
  57.             let v = self.bars[i]
  58.            
  59.             UIView.animate(withDuration: 0.2, animations: {
  60.                 v.frame.origin.y = y
  61.                 v.frame.size.height = value_height
  62.             })
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement