Skip to content

Instantly share code, notes, and snippets.

@jamesnvc
Created February 17, 2016 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesnvc/3645b8a291b9aa74504d to your computer and use it in GitHub Desktop.
Save jamesnvc/3645b8a291b9aa74504d to your computer and use it in GitHub Desktop.
Core Animation demo
//
// ViewController.swift
// AnimDemo
//
// Created by James Cash on 17-02-16.
// Copyright © 2016 Occasionally Cogent. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let sublayer = CALayer()
sublayer.frame = CGRect(x: 50, y: 100, width: 100, height: 100)
sublayer.backgroundColor = UIColor.greenColor().CGColor
self.view.layer.addSublayer(sublayer)
sublayer.speed = 2
// let posAnim = CABasicAnimation(keyPath: "position")
// posAnim.fromValue = NSValue(CGPoint: CGPoint(x: 50, y: 100))
// posAnim.toValue = NSValue(CGPoint: CGPoint(x: 100, y: 200))
// posAnim.duration = 1.0
// posAnim.autoreverses = true
// posAnim.repeatCount = HUGE
// sublayer.addAnimation(posAnim, forKey: "posAnim")
// let yPosAnim = CABasicAnimation(keyPath: "position.y")
// yPosAnim.fromValue = 100
// yPosAnim.toValue = 200
// yPosAnim.duration = 1.0
// yPosAnim.autoreverses = true
// yPosAnim.repeatCount = HUGE
// sublayer.addAnimation(yPosAnim, forKey: "posAnim")
// let colorAnim = CABasicAnimation(keyPath: "backgroundColor")
// colorAnim.fromValue = UIColor.greenColor().CGColor
// colorAnim.toValue = UIColor.redColor().CGColor
// colorAnim.duration = 2.0
// colorAnim.autoreverses = true
// colorAnim.repeatCount = HUGE
// sublayer.addAnimation(colorAnim, forKey: "colorAnim")
let colorAnim = CAKeyframeAnimation(keyPath: "backgroundColor")
colorAnim.values = [
UIColor.greenColor().CGColor,
UIColor.blueColor().CGColor,
UIColor.redColor().CGColor
]
colorAnim.keyTimes = [
0,
0.2,
1
]
colorAnim.duration = 2.0
colorAnim.autoreverses = true
colorAnim.repeatCount = HUGE
sublayer.addAnimation(colorAnim, forKey: "colorAnim")
let posPath = UIBezierPath()
posPath.moveToPoint(CGPoint(x: 50, y: 100))
posPath.addLineToPoint(CGPoint(x: 100, y: 200))
posPath.addArcWithCenter(CGPoint(x: 150, y:150), radius: 50, startAngle: 1, endAngle: 3, clockwise: true)
let posAnim = CAKeyframeAnimation(keyPath: "position")
posAnim.path = posPath.CGPath
posAnim.duration = 2.0
posAnim.autoreverses = true
posAnim.repeatCount = 2
posAnim.delegate = self
sublayer.addAnimation(posAnim, forKey: "pathAnim")
}
override func animationDidStart(anim: CAAnimation) {
print("Started animation \(anim)")
}
override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
print("stoped animation \(anim)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment