         path.lineWidth = lineWidth
         return path
     }
-
+    
+    private func pathForVerticalLine(originY: CGFloat, height: CGFloat, positionX: CGFloat) -> UIBezierPath
+    {
+        let path = UIBezierPath()
+        path.moveToPoint(CGPoint(x: positionX, y: originY))
+        path.addLineToPoint(CGPoint(x: positionX, y: originY + height))
+        path.lineWidth = lineWidth
+        return path
+    }
+    
     private func pathForCircleCenteredAtPoint(midPoint: CGPoint, withRadius radius: CGFloat) -> UIBezierPath
     {
         let path = UIBezierPath(
             arcCenter: midPoint,
             radius: radius,
             startAngle: 0.0,
-            endAngle: CGFloat(2*M_PI),
+            endAngle: CGFloat(2 * M_PI),
             clockwise: false
         )
         path.lineWidth = lineWidth
         return path
     }
-
-
+    
     /*
-    // Only override drawRect: if you perform custom drawing.
-    // An empty implementation adversely affects performance during animation.
-    override func drawRect(rect: CGRect) {
-        // Drawing code
-    }
-    */
-    
-    //MARK: scrollView的代理方法
+     // Only override drawRect: if you perform custom drawing.
+     // An empty implementation adversely affects performance during animation.
+     override func drawRect(rect: CGRect) {
+     // Drawing code
+     }
+     */
+    
+    // MARK: scrollView的代理方法
     
     func scrollViewDidScroll(scrollView: UIScrollView) {
         setNeedsDisplay()
     }
-
+    
 }
 

            path.stroke()
        }
        
        //画点
        var positionPressed = [Float].init(count: 5, repeatedValue: 0)
        typealias Note = CGSSBeatmap.Note
        var sliders = [Int: Note]()
        for note in notes {
            if note.finishPos != 0  {
                let pointY = getPointY(note.sec!)
                //未到达显示区域时 continue
                if pointY > rect.origin.y + rect.size.height {
                    if note.type == 2 {
                        if positionPressed[note.finishPos!-1] == 0 {
                            positionPressed[note.finishPos!-1] = note.sec!
                        } else {
                            positionPressed[note.finishPos!-1] = 0
                        }
                    }
                    if note.status > 0 && positionPressed[note.finishPos!-1] != 0 {
                        positionPressed[note.finishPos!-1] = 0
                    }
                    continue
                }
                //超出当前显示区域时 退出循环
                if pointY < rect.origin.y { break }
                let path = pathForPoint(note.finishPos!, sec: note.sec!)
                strokeColor.set()
                //path.stroke()
                path.fill()
                
                //简单长按类型
                if note.type == 2 {
                    if positionPressed[note.finishPos!-1] == 0 {
                        positionPressed[note.finishPos!-1] = note.sec!
                    } else {
                        let path = pathForLongPress(note.finishPos!, sec1: positionPressed[note.finishPos!-1], sec2: note.sec!)
                        positionPressed[note.finishPos!-1] = 0
                        strokeColor.colorWithAlphaComponent(0.5).set()
                        //path.stroke()
                        path.fill()
                    }
                }
                //长按以滑动结束的情况
                if note.status > 0 && positionPressed[note.finishPos!-1] != 0 {
                    let path = pathForLongPress(note.finishPos!, sec1: positionPressed[note.finishPos!-1], sec2: note.sec!)
                    positionPressed[note.finishPos!-1] = 0
                    strokeColor.colorWithAlphaComponent(0.5).set()
                    //path.stroke()
                    path.fill()
                }
            }
        }
        
        for note in notes {
            if note.finishPos != 0  {
                let pointY = getPointY(note.sec!)
                //未到达显示区域时 continue
                if pointY > rect.origin.y + rect.size.height + BeatmapView.sectionHeight {
                    continue
                }
                //超出当前显示区域时 退出循环
                if pointY < rect.origin.y - BeatmapView.sectionHeight { break }
                
                //滑条
                if note.groupId != 0 {
                    if (sliders[note.groupId!] != nil) {
                        let path = pathForSlider(sliders[note.groupId!]!, note2: note)
                        sliders[note.groupId!] = note
                        strokeColor.colorWithAlphaComponent(0.5).set()
                        //path.stroke()
                        path?.fill()
                    } else {
                        sliders[note.groupId!] = note
                    }
                }
            }
        }
        
        for note in notes {
            if note.finishPos != 0  {
                let pointY = getPointY(note.sec!)
                //未到达显示区域时 continue
                if pointY > rect.origin.y + rect.size.height {
                    continue
                }
                //超出当前显示区域时 退出循环
                if pointY < rect.origin.y { break }

                //箭头向左
                if note.status == 1 {
                    let center = CGPointMake(getPointX(note.finishPos!), getPointY(note.sec!))
                    let point1 = CGPointMake(center.x - BeatmapView.noteRadius + 1, center.y)


import UIKit

class BeatmapView: UIScrollView, UIScrollViewDelegate {
    
    static let widthInset: CGFloat = 100
    static let sectionHeight: CGFloat = 245
    static let heightInset: CGFloat = 60
    static let noteRadius: CGFloat = 7
    
    var beatmap: CGSSBeatmap!
    var notes: [CGSSBeatmap.Note]!
    var bpm: Int!
    var interval: CGFloat {
        return (CGSSTool.width - 2 * BeatmapView.widthInset) / 4
    }
    
    var secScale: CGFloat {
        return BeatmapView.sectionHeight * (CGFloat(bpm) / 60 / 4)
    }
    
    var strokeColor: UIColor!
    
    private var lineWidth: CGFloat = 1
    func getPointX(position: Int) -> CGFloat {
        return BeatmapView.widthInset + interval * (CGFloat(position) - 1)
    }
    
    func getPointY(sec: Float) -> CGFloat {
        return contentSize.height - CGFloat(sec - self.beatmap.preSeconds!) * secScale - BeatmapView.heightInset
    }
    
    override func drawRect(rect: CGRect) {
        typealias Note = CGSSBeatmap.Note
        // 画纵向辅助线
        for i in 1...5 {
            let path = pathForVerticalLine(rect.origin.y, height: rect.size.height, positionX: getPointX(i))
            UIColor.lightGrayColor().colorWithAlphaComponent(0.5).set()
            path.stroke()
        }
        
        // 画小节线
        let halfquadSectionMax = Int(((contentSize.height - rect.origin.y - BeatmapView.heightInset) / BeatmapView.sectionHeight * 8))
        let halfquadSectionMin = Int(((contentSize.height - frame.size.height - rect.origin.y - BeatmapView.heightInset) / BeatmapView.sectionHeight * 8))
        for i in halfquadSectionMin...halfquadSectionMax {
            let pointY = contentSize.height - BeatmapView.heightInset - CGFloat(i) * BeatmapView.sectionHeight / 8
            if i % 8 == 0 {
                UIColor.darkGrayColor().set()
                let sectionNumber: NSString = NSString.init(format: "%03d", i / 8)
                let attDict = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: UIColor.darkGrayColor()]
                sectionNumber.drawAtPoint(CGPointMake(25, pointY - 7), withAttributes: attDict)
                let comboNumber: NSString = NSString.init(format: "%d", beatmap.comboForSec(Float(i / 8) / (Float(bpm) / 60 / 4)))
                comboNumber.drawAtPoint(CGPointMake(CGSSTool.width - 46, pointY - 7), withAttributes: attDict)
            }
            else if i % 2 == 0 { UIColor.lightGrayColor().set() }
            else { UIColor.lightGrayColor().colorWithAlphaComponent(0.5).set() }
