Skip to content

Instantly share code, notes, and snippets.

@mmuszynski
Last active August 29, 2015 14:22
Show Gist options
  • Save mmuszynski/ae811aba51d05d3ce131 to your computer and use it in GitHub Desktop.
Save mmuszynski/ae811aba51d05d3ce131 to your computer and use it in GitHub Desktop.
//
// MusicStaffViewStaffLayer.swift
// MusicStaffView
//
// Created by Mike Muszynski on 1/4/15.
// Copyright (c) 2015 Mike Muszynski. All rights reserved.
//
import UIKit
class MusicStaffViewStaffLayer: CAShapeLayer {
var maxLedgerLines : Int = 0
var currentHorizontalPosition : CGFloat {
get {
let sublayers = self.sublayers
guard sublayers != nil else {
return 0
}
let lastElement = sublayers!.last as CALayer!
return lastElement.frame.origin.x + lastElement.frame.size.width
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
strokeColor = UIColor.blackColor().CGColor
}
override init() {
super.init()
strokeColor = UIColor.blackColor().CGColor
}
override var path : CGPath? {
get {
return staffPath()
}
set {
}
}
func staffPath() -> CGPathRef {
let staffLines = UIBezierPath()
let spaceWidth : CGFloat = self.bounds.size.height / (6.0 + 2.0 * CGFloat(maxLedgerLines))
self.lineWidth = spaceWidth / 10.0
for i in 1...(5 + maxLedgerLines) {
if (i <= maxLedgerLines || i > 5 + maxLedgerLines) {
continue
}
let height = self.bounds.origin.y + spaceWidth * CGFloat(i)
staffLines.moveToPoint(CGPointMake(self.bounds.origin.x, height))
staffLines.addLineToPoint(CGPointMake(self.bounds.origin.x + self.bounds.size.width, height))
}
return staffLines.CGPath
}
}
@lilyball
Copy link

import UIKit

class MyShapeLayer: CAShapeLayer {
    override var path: CGPath? {
        get {
            return staffPath()
        }
        set {

        }
    }

    private func staffPath() -> CGPath? {
        return nil
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment