Skip to content

Instantly share code, notes, and snippets.

@odrobnik
Last active August 29, 2015 14:24
Show Gist options
  • Save odrobnik/6541355774b7727e6de5 to your computer and use it in GitHub Desktop.
Save odrobnik/6541355774b7727e6de5 to your computer and use it in GitHub Desktop.
//
// 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