Last active
August 29, 2015 14:24
-
-
Save odrobnik/6541355774b7727e6de5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIBezierPath+Ring.swift | |
// WheelBase | |
// | |
// Created by Oliver Drobnik on 13.07.15. | |
// Copyright (c) 2015 Cocoanetics. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import Darwin | |
extension UIBezierPath | |
{ | |
convenience init(ringSegmentWithCenter center: CGPoint, innerRadius: CGFloat, outerRadius: CGFloat, startAngle: CGFloat, endAngle: CGFloat) | |
{ | |
// helper function to determine a location | |
func _positionAtAngle(center: CGPoint, angle: CGFloat, radius: CGFloat) -> CGPoint | |
{ | |
var dx = radius * sin(angle) | |
var dy = radius * cos(angle) | |
return CGPoint(x: center.x + dx, y: center.y - dy) | |
} | |
self.init() | |
let ringWidth = outerRadius - innerRadius; | |
// addArc has angles rotated 90 degrees CCW | |
let startArcAngle = startAngle - CGFloat(M_PI_2) | |
let endArcAngle = endAngle - CGFloat(M_PI_2) | |
// start cap | |
let middleRadius = (outerRadius + innerRadius)/2.0; | |
let middleStartPoint = _positionAtAngle(center, startAngle, middleRadius) | |
self.addArcWithCenter(middleStartPoint, radius: ringWidth/2.0, startAngle: startArcAngle + CGFloat(M_PI), endAngle: startArcAngle, clockwise: true) | |
// outer arc | |
self.addArcWithCenter(center, radius: outerRadius, startAngle: startArcAngle, endAngle: endArcAngle, clockwise: true) | |
// end cap | |
let middleEndPoint = _positionAtAngle(center, endAngle, middleRadius) | |
self.addArcWithCenter(middleEndPoint, radius: ringWidth/2.0, startAngle: endArcAngle, endAngle: endAngle+CGFloat(M_PI_2), clockwise: true) | |
// inner arc | |
self.addArcWithCenter(center, radius: innerRadius, startAngle: endArcAngle, endAngle: startArcAngle, clockwise: false) | |
closePath() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment