Last active
May 6, 2016 03:52
-
-
Save stonetip/d766e17ca02452d12c010c7d5ead44d6 to your computer and use it in GitHub Desktop.
iOS Swift playground to build a donut arc (such as for a graph)
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
import UIKit | |
// all angles need to be converted to radians | |
func Degrees2Radians(degrees: Double) -> CGFloat{ | |
return CGFloat( degrees * M_PI / 180) | |
} | |
// set width and height that will be used by the view and layers | |
let width = 600 | |
let height = 400 | |
// values that will be used to draw the arc | |
var lineWidth = width / 4 | |
var radius = width / 2 | |
// tweak values if height is less than width | |
if(width >= height){ | |
lineWidth = height / 4 | |
radius = height / 2 | |
} | |
// make the arc fit and look pretty | |
radius -= lineWidth / 2 | |
let bounds = CGRect(x: 0, y: 0, width: width, height: height) | |
let view = UIView(frame: bounds) | |
// provide a shape layer and set its path to the arc | |
let shapeLayer = CAShapeLayer() | |
shapeLayer.frame = bounds // need this for the rotation transform to work correctly | |
view.layer.addSublayer(shapeLayer) | |
// define a start and end angle for the test arc | |
var startAngle = Degrees2Radians(0) | |
var endAngle = Degrees2Radians(180) | |
// create the arc | |
shapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: width / 2, y: height / 2), radius: CGFloat(radius), startAngle: startAngle, endAngle: endAngle, clockwise: true).CGPath | |
// give the layer some properties | |
shapeLayer.backgroundColor = UIColor.yellowColor().CGColor | |
shapeLayer.fillColor = UIColor.clearColor().CGColor | |
shapeLayer.strokeColor = UIColor.redColor().CGColor | |
shapeLayer.lineWidth = CGFloat(lineWidth) | |
// first rotation angle | |
var rotationAngle = Degrees2Radians(-90) | |
// apply the rotation | |
shapeLayer.transform = CATransform3DRotate(shapeLayer.transform, rotationAngle, 0.0, 0.0, 1.0) | |
// second rotation angle | |
rotationAngle = Degrees2Radians(45) | |
// apply - this transformation is cumulative | |
shapeLayer.transform = CATransform3DRotate(shapeLayer.transform, rotationAngle, 0.0, 0.0, 1.0) | |
// Test using a different angle to see if if the donut redraws completely | |
endAngle = Degrees2Radians(135) | |
shapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: width / 2, y: height / 2), radius: CGFloat(radius), startAngle: startAngle, endAngle: endAngle, clockwise: true).CGPath | |
// this gives us a handy way to see the final results | |
view.backgroundColor = UIColor.lightGrayColor() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment