Skip to content

Instantly share code, notes, and snippets.

@Jesse-calkin
Created May 19, 2015 14:37
Show Gist options
  • Save Jesse-calkin/79e74c019151dbad1a96 to your computer and use it in GitHub Desktop.
Save Jesse-calkin/79e74c019151dbad1a96 to your computer and use it in GitHub Desktop.
Core Animation + UIKit Dynamics
//
// ViewController.swift
// Animation Sandbox
//
// Created by Jesse Calkin on 5/18/15.
// Copyright (c) 2015 Shoshin Boogie. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var start = CGPoint()
var end = CGPoint()
var animator = UIDynamicAnimator()
var path = UIBezierPath()
var path3 = UIBezierPath()
var yPos: Double {
get {
return Double(arc4random_uniform(100))
}
}
var line = CAShapeLayer()
override func viewDidLoad() {
super.viewDidLoad()
start = CGPoint(x: 0.0, y: view.frame.height / 2)
end = CGPoint(x: view.frame.width, y: view.frame.height / 2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
setupAnimation()
overlayView.center = CGPoint(x: view.center.x, y: 0.0)
overlayView.layer.cornerRadius = 25.0
dynamicThisShit()
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
var touch = touches.first as! UITouch
resetBox(touch)
followTheBox()
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesMoved(touches, withEvent: event)
var touch = touches.first as! UITouch
resetBox(touch)
followTheBox()
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
dynamicThisShit()
}
func bendToTouch(touch: UITouch) {
var touchPath = UIBezierPath()
touchPath.moveToPoint(start)
touchPath.addCurveToPoint(end, controlPoint1: start, controlPoint2: CGPoint(x: touch.locationInView(view).x, y: touch.locationInView(view).y))
line.path = touchPath.CGPath
}
func resetTouch() {
line.path = path.CGPath
}
func resetBox(touch: UITouch) {
overlayView.center.y = touch.locationInView(view).y
}
func setupAnimation () {
path.moveToPoint(start)
path.addCurveToPoint(end, controlPoint1: start, controlPoint2: end)
line.strokeColor = UIColor.cyanColor().CGColor
line.fillColor = UIColor.clearColor().CGColor
line.lineWidth = 10
line.lineCap = kCALineCapRound
line.path = path.CGPath
view.layer.addSublayer(line)
}
func animate() {
var animation = CABasicAnimation(keyPath: "path")
animation.fromValue = path
animation.toValue = path3
animation.duration = 1
animation.autoreverses = true
animation.repeatCount = 1000
line.addAnimation(animation, forKey: "path")
}
func followTheBox() {
animator.removeAllBehaviors()
var pointToFollow = overlayView.center
var fPath = UIBezierPath()
fPath.moveToPoint(start)
fPath.addCurveToPoint(end, controlPoint1: start, controlPoint2: CGPoint(x: pointToFollow.x, y: pointToFollow.y))
line.path = fPath.CGPath
}
func dynamicThisShit() {
animator = UIDynamicAnimator(referenceView: view)
let action = {() -> Void in
let fPath = UIBezierPath()
fPath.moveToPoint(self.start)
fPath.addCurveToPoint(self.end, controlPoint1: self.start, controlPoint2: CGPoint(x: self.overlayView.center.x, y: self.overlayView.center.y))
CATransaction.begin()
CATransaction.setDisableActions(true)
self.line.path = fPath.CGPath
CATransaction.commit()
}
let snap = UISnapBehavior(item: overlayView, snapToPoint: view.center)
snap.action = action
snap.damping = 0.1
let gravity = UIGravityBehavior(items: [overlayView])
gravity.action = action
let bouncy = UIDynamicItemBehavior(items: [overlayView])
bouncy.elasticity = 1.75
let collision = UICollisionBehavior(items: [overlayView])
collision.translatesReferenceBoundsIntoBoundary = true
animator.addBehavior(snap)
// animator.addBehavior(collision)
// animator.addBehavior(bouncy)
}
@IBOutlet var overlayView: UIView!
func addMaskLayer() {
//// Bezier 2 Drawing
var bezier2Path = UIBezierPath()
bezier2Path.moveToPoint(view.bounds.origin)
bezier2Path.addCurveToPoint(CGPoint(x: 69.5, y: 14.5), controlPoint1: CGPoint(x: 86, y: 3.5), controlPoint2: CGPoint(x: 69.5, y: 14.5))
bezier2Path.addLineToPoint(CGPoint(x: 69.5, y: 33.5))
bezier2Path.addLineToPoint(CGPoint(x: 139.5, y: 33.5))
bezier2Path.addLineToPoint(CGPoint(x: 139.5, y: 14.5))
bezier2Path.addCurveToPoint(CGPoint(x: 103.5, y: 3.5), controlPoint1: CGPoint(x: 139.5, y: 14.5), controlPoint2: CGPoint(x: 121, y: 3.5))
bezier2Path.closePath()
var maskLayer = CAShapeLayer()
maskLayer.path = bezier2Path.CGPath
overlayView.layer.mask = maskLayer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment