Skip to content

Instantly share code, notes, and snippets.

@crenwick
Created November 4, 2015 15:08
Show Gist options
  • Save crenwick/7cbe5a1acd0d7691aa79 to your computer and use it in GitHub Desktop.
Save crenwick/7cbe5a1acd0d7691aa79 to your computer and use it in GitHub Desktop.
Animation Playground Boilerplate
// Code from:
// https://possiblemobile.com/2015/03/prototyping-uiview-animations-swift-playground/
// &
// http://mathewsanders.com/animations-in-swift-part-two/
import UIKit
import XCPlayground
let startingColor = UIColor(red: (253.0/255.0), green: (159.0/255.0), blue: (47.0/255.0), alpha: 1.0)
let endingColor = UIColor(red: (255.0/255.0), green: (61.0/255.0), blue: (24.0/255.0), alpha: 1.0)
let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 300.0, height: 300.0))
XCPShowView("Container View", view: containerView)
let ovalStartAngle = CGFloat(90.01 * M_PI/180)
let ovalEndAngle = CGFloat(90 * M_PI/180)
let ovalRect = CGRectMake(100, 100, 100, 100)
// create the bezier path
let ovalPath = UIBezierPath()
ovalPath.addArcWithCenter(CGPointMake(CGRectGetMidX(ovalRect), CGRectGetMidY(ovalRect)),
radius: CGRectGetWidth(ovalRect) / 2,
startAngle: ovalStartAngle,
endAngle: ovalEndAngle, clockwise: true)
// create an object that represents how the curve
// should be presented on the screen
let progressLine = CAShapeLayer()
progressLine.path = ovalPath.CGPath
progressLine.strokeColor = UIColor.blueColor().CGColor
progressLine.fillColor = UIColor.clearColor().CGColor
progressLine.lineWidth = 10.0
progressLine.lineCap = kCALineCapRound
// add the curve to the screen
containerView.layer.addSublayer(progressLine)
// create a basic animation that animates the value 'strokeEnd'
// from 0.0 to 1.0 over 3.0 seconds
let animateStrokeEnd = CABasicAnimation(keyPath: "strokeEnd")
animateStrokeEnd.duration = 3.0
animateStrokeEnd.fromValue = 0.0
animateStrokeEnd.toValue = 1.0
progressLine.addAnimation(animateStrokeEnd, forKey: "animate stroke end animation")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment