Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Creates a series of images for an animation similar to UIActivityIndicatorView
import UIKit
private extension CGPoint {
/// Multiplies point by given value
/// - parameter value: The x and y coordinates are multipled by this value.
/// - returns: A new CGPoint whose coordinate components have been multiplied by `value`
func multipliedBy(value: CGFloat) -> CGPoint {
return CGPoint(x: self.x * value, y: self.y * value)
/// Creates an array of images for a spinning wait indicator.
/// On iOS, we have UIActivityIndicatorView, but since this is unavailble on WatchOS,
/// we can create a similar-looking view by passing an animated UIImage to WKInterfaceImage.
/// - parameter radius: Radius of the spinning indicator.
/// - returns: An array of images to be used in a spinning wait indicator animation.
/// The width and height of the generated images will be 2x the given radius.
func generateSpinningWaitImagesWithRadius(radius: CGFloat) -> [UIImage] {
let startRadius = radius * 0.5
let lineWidth = radius / 5
let endRadius = radius - lineWidth / 2
let diameter = radius * 2
let ticks = (0..<12).map { (tickIndex: Int) -> CGFloat in
return CGFloat(Double(tickIndex) * (M_PI / 6))
return { (angle: CGFloat) -> UIImage in
UIGraphicsBeginImageContextWithOptions(CGSize(width: diameter, height: diameter), false, 0)
defer {
for tick in ticks {
UIColor(white: tick / ticks.last!, alpha: 1).setStroke()
let unitPoint = CGPoint(x: cos(tick), y: sin(tick))
let tickMark = UIBezierPath()
tickMark.lineWidth = lineWidth
tickMark.lineCapStyle = .Round
tickMark.lineJoinStyle = .Round
tickMark.applyTransform(CGAffineTransformMakeTranslation(radius, radius))
return UIGraphicsGetImageFromCurrentImageContext()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment