Skip to content

Instantly share code, notes, and snippets.

@crenwick
Created November 5, 2015 16:54
Show Gist options
  • Save crenwick/0c129d77ccef973e8e13 to your computer and use it in GitHub Desktop.
Save crenwick/0c129d77ccef973e8e13 to your computer and use it in GitHub Desktop.
Draw a path with a semicircle, animation movement across it.
// Follow the horizon
import UIKit
import XCPlayground
import SpriteKit
let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 300.0))
let view = SKView(frame: CGRectMake(0, 0, 375, 120))
XCPlaygroundPage.currentPage.liveView = view
// create the scene
let scene = SKScene(size: CGSizeMake(375, 120))
scene.scaleMode = .AspectFit
view.presentScene(scene)
// draw a long line
let longLine = UIBezierPath()
longLine.moveToPoint(CGPointMake(0, 25))
longLine.addLineToPoint(CGPointMake(375, 25))
scene.addChild(SKShapeNode(path: longLine.CGPath))
// add the first straight line
let line1 = UIBezierPath()
line1.moveToPoint(CGPointMake(0, 25))
line1.addLineToPoint(CGPointMake(75, 25))
// make the curved line
let line2 = UIBezierPath()
line2.moveToPoint(CGPointMake(75, 25))
line2.addCurveToPoint(CGPointMake(187.5, 100), controlPoint1: CGPointMake(75, 40), controlPoint2: CGPointMake(100, 100))
line2.addCurveToPoint(CGPointMake(300, 25), controlPoint1: CGPointMake(270, 100), controlPoint2: CGPointMake(300, 40))
line2.miterLimit = 4
line2.lineCapStyle = .Round
// make it dashed
let line2Dashed = SKShapeNode(path: CGPathCreateCopyByDashingPath(line2.CGPath, nil, 0, [4, 4], 2)!)
scene.addChild(line2Dashed)
// add the last straight line
let line3 = UIBezierPath()
line3.moveToPoint(CGPointMake(300, 25))
line3.addLineToPoint(CGPointMake(375, 25))
// make a red box
let redBox = SKSpriteNode(color: SKColor.redColor(), size: CGSizeMake(10, 10))
scene.addChild(redBox)
// Make the action
let followLine1 = SKAction.followPath(line1.CGPath, asOffset: false, orientToPath: true, duration: 0.5)
let followLine2 = SKAction.followPath(line2.CGPath, asOffset: false, orientToPath: true, duration: 3)
let followLine3 = SKAction.followPath(line3.CGPath, asOffset: false, orientToPath: true, duration: 0.5)
redBox.runAction(SKAction.sequence([followLine1, followLine2, followLine3]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment