Skip to content

Instantly share code, notes, and snippets.

@sketchytech
Forked from crenwick/AnimationPlayground.swift
Created January 14, 2017 12:12
Show Gist options
  • Save sketchytech/d224a33641fb53ef827d55c1170871f7 to your computer and use it in GitHub Desktop.
Save sketchytech/d224a33641fb53ef827d55c1170871f7 to your computer and use it in GitHub Desktop.
Swift 3: Draw a path with a semicircle, animation movement across it.
// Follow the horizon
import UIKit
import PlaygroundSupport
import SpriteKit
let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 300.0))
let view = SKView(frame: CGRect(x:0, y:0, width:375, height:120))
PlaygroundPage.current.liveView = view
// create the scene
let scene = SKScene(size: CGSize(width:375, height:120))
scene.scaleMode = .aspectFit
view.presentScene(scene)
// draw a long line
let longLine = UIBezierPath()
longLine.move(to: CGPoint(x:0, y:25))
longLine.addLine(to: CGPoint(x:375, y:25))
scene.addChild(SKShapeNode(path: longLine.cgPath))
// add the first straight line
let line1 = UIBezierPath()
line1.move(to: CGPoint(x:0, y:25))
line1.addLine(to: CGPoint(x:75, y:25))
// make the curved line
let line2 = UIBezierPath()
line2.move(to: CGPoint(x:75, y:25))
line2.addCurve(to: CGPoint(x:187.5, y:100), controlPoint1: CGPoint(x:75, y:40), controlPoint2: CGPoint(x:100, y:100))
line2.addCurve(to: CGPoint(x:300, y:25), controlPoint1: CGPoint(x:270, y:100), controlPoint2: CGPoint(x:300, y:40))
line2.miterLimit = 4
line2.lineCapStyle = .round
// make it dashed
let line2Dashed = SKShapeNode(path: line2.cgPath.copy(dashingWithPhase: 2, lengths: [4, 4]))
scene.addChild(line2Dashed)
// add the last straight line
let line3 = UIBezierPath()
line3.move(to: CGPoint(x:300, y:25))
line3.addLine(to: CGPoint(x:375, y:25))
// make a red box
let redBox = SKSpriteNode(color: SKColor.red, size: CGSize(width:10, height:10))
scene.addChild(redBox)
// Make the action
let followLine1 = SKAction.follow(line1.cgPath, asOffset: false, orientToPath: true, duration: 0.5)
let followLine2 = SKAction.follow(line2.cgPath, asOffset: false, orientToPath: true, duration: 3)
let followLine3 = SKAction.follow(line3.cgPath, asOffset: false, orientToPath: true, duration: 0.5)
redBox.run(SKAction.sequence([followLine1, followLine2, followLine3]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment