Last active
April 13, 2018 14:35
-
-
Save sunnycyk/29f2403e0a5c53420a20 to your computer and use it in GitHub Desktop.
IOS Core Animation Example Chapter 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// animation810 | |
// | |
// Created by Sunny Cheung on 27/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet var containerView:UIView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
// create a path | |
var bezierPath:UIBezierPath = UIBezierPath() | |
bezierPath.moveToPoint(CGPointMake(0,150)) | |
bezierPath.addCurveToPoint(CGPointMake(300,150), controlPoint1: CGPointMake(75,0), controlPoint2: CGPointMake(225,300)) | |
// draw the path using a CAShapeLayer | |
var pathLayer:CAShapeLayer = CAShapeLayer() | |
pathLayer.path = bezierPath.CGPath | |
pathLayer.fillColor = UIColor.clearColor().CGColor | |
pathLayer.strokeColor = UIColor.redColor().CGColor | |
pathLayer.lineWidth = 3.0 | |
self.containerView.layer.addSublayer(pathLayer) | |
// add a colored layer | |
var colorLayer:CALayer = CALayer() | |
colorLayer.frame = CGRectMake(0,0,64,64) | |
colorLayer.position = CGPointMake(0,150) | |
colorLayer.backgroundColor = UIColor.greenColor().CGColor | |
self.containerView.layer.addSublayer(colorLayer) | |
// create the position animation | |
var animation1:CAKeyframeAnimation = CAKeyframeAnimation() | |
animation1.keyPath = "position" | |
animation1.path = bezierPath.CGPath | |
animation1.rotationMode = kCAAnimationRotateAuto | |
// Create color animation | |
var animation2:CABasicAnimation = CABasicAnimation() | |
animation2.keyPath = "backgroundColor" | |
animation2.toValue = UIColor.redColor().CGColor | |
// create group animation | |
var groupAnimation:CAAnimationGroup = CAAnimationGroup() | |
groupAnimation.animations = [animation1, animation2] | |
groupAnimation.duration = 4.0 | |
colorLayer.addAnimation(groupAnimation, forKey: nil) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// animation811 | |
// | |
// Created by Sunny Cheung on 27/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet var imageView:UIImageView! | |
var images:Array<UIImage>! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
// set up images | |
self.images = [ UIImage(named: "Anchor.png")!, | |
UIImage(named: "Cone.png")!, | |
UIImage(named: "Igloo.png")!, | |
self.imageView.image!] // UIImage(named: "Spaceship.png") | |
// note: Image on storyboard will create different object? | |
} | |
@IBAction func switchImage() { | |
//set up crossfade transistion | |
var transition:CATransition = CATransition() | |
transition.type = kCATransitionFade | |
// apply transition to imageview backing layer | |
self.imageView.layer.addAnimation(transition, forKey: nil) | |
// cycle to next image | |
var currentImage:UIImage = self.imageView.image! | |
var index:Int = (self.images as NSArray).indexOfObjectIdenticalTo(currentImage) | |
index = (index + 1) % self.images.count | |
self.imageView.image = self.images[index] | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppDelegate.swift | |
// animation812 | |
// | |
// Created by Sunny Cheung on 29/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate { | |
var window: UIWindow? | |
var tabBarController:UITabBarController! | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
// Override point for customization after application launch. | |
self.window = UIWindow(frame: UIScreen.mainScreen().bounds) | |
let viewController1:UIViewController = FirstViewController(nibName: "FirstViewController", bundle: nil) | |
let viewController2:UIViewController = SecondViewController(nibName: "SecondViewController", bundle: nil) | |
self.tabBarController = UITabBarController() | |
self.tabBarController.viewControllers = [viewController1, viewController2] | |
self.tabBarController.delegate = self | |
self.window?.rootViewController = self.tabBarController | |
self.window?.makeKeyAndVisible() | |
return true | |
} | |
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) { | |
var transition:CATransition = CATransition() | |
transition.type = kCATransitionFade | |
self.tabBarController.view.layer.addAnimation(transition, forKey: nil) | |
} | |
} | |
// | |
// FirstViewController.swift | |
// animation812 | |
// | |
// Created by Sunny Cheung on 29/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
class FirstViewController : UIViewController { | |
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { | |
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) | |
self.title = NSLocalizedString("First", comment: "First") | |
self.tabBarItem.image = UIImage(named: "first") | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder:aDecoder) | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
} | |
// | |
// SecondViewController.swift | |
// animation812 | |
// | |
// Created by Sunny Cheung on 29/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
class SecondViewController : UIViewController { | |
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { | |
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) | |
self.title = NSLocalizedString("Second", comment: "Second") | |
self.tabBarItem.image = UIImage(named: "second") | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder:aDecoder) | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// animation813 | |
// | |
// Created by Sunny Cheung on 29/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet var imageView:UIImageView! | |
var images:Array<UIImage>! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
self.images = [UIImage(named: "Anchor")!, UIImage(named:"Cone")!, UIImage(named:"Igloo")!, self.imageView.image!] | |
} | |
@IBAction func switchImage() { | |
UIView.transitionWithView(self.imageView, duration: 1.0, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: { | |
let currentImage:UIImage = self.imageView.image! | |
var index:Int = (self.images as NSArray).indexOfObject(currentImage) | |
index = (index + 1) % self.images.count | |
self.imageView.image = self.images[index] | |
}, completion: nil) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// animation814 | |
// | |
// Created by Sunny Cheung on 29/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBAction func performTransition() { | |
// preserve the current view snapshot | |
UIGraphicsBeginImageContextWithOptions(CGSize(width: self.view.bounds.width, height:self.view.bounds.height), true, 0.0) | |
self.view.layer.renderInContext(UIGraphicsGetCurrentContext()) | |
let coverImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() | |
// insert snapshot view in front of this one | |
let coverView:UIView = UIImageView(image: coverImage) | |
coverView.frame = self.view.bounds | |
self.view.addSubview(coverView) | |
// update the view (we'll simply randomize the layer background color | |
var red:CGFloat = CGFloat(arc4random()) / CGFloat(INT_MAX) | |
var green:CGFloat = CGFloat(arc4random())/CGFloat(INT_MAX) | |
var blue:CGFloat = CGFloat(arc4random())/CGFloat(INT_MAX) | |
self.view.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0) | |
// perform animation | |
UIView.animateWithDuration(1.0, animations: { | |
// scale, rotate and fade the view | |
var transform:CGAffineTransform = CGAffineTransformMakeScale(0.01, 0.01) | |
transform = CGAffineTransformRotate(transform, CGFloat( M_PI_2)) | |
coverView.transform = transform | |
coverView.alpha = 0.0 | |
}, completion: { | |
(finished:Bool) in | |
// remover the cover view now we're finish with it | |
coverView.removeFromSuperview() | |
}) | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// animation815 | |
// | |
// Created by Sunny Cheung on 29/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet var containerView:UIView! | |
var shipLayer:CALayer! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
self.shipLayer = CALayer() | |
self.shipLayer.frame = CGRectMake(0,0,128,128) | |
self.shipLayer.position = CGPointMake(150,150) | |
self.shipLayer.contents = UIImage(named:"Ship")?.CGImage | |
self.containerView.layer.addSublayer(self.shipLayer) | |
} | |
@IBAction func start() { | |
var animation:CABasicAnimation = CABasicAnimation() | |
animation.keyPath = "transform.rotation" | |
animation.duration = 2.0 | |
animation.byValue = M_PI * 2 | |
animation.delegate = self | |
self.shipLayer.addAnimation(animation, forKey: "rotateAnimation") | |
} | |
@IBAction func stop() { | |
self.shipLayer.removeAnimationForKey("rotateAnimation") | |
} | |
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) { | |
NSLog("The animation stopped (finished: %@", flag ? "YES": "NO") | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// animation82 | |
// | |
// Created by Sunny Cheung on 25/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet var layerView:UIView! | |
var colorLayer:CALayer! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
// create sub layer | |
self.colorLayer = CALayer() | |
self.colorLayer.frame = CGRectMake(50.0, 50.0, 100.0, 100.0) | |
self.colorLayer.backgroundColor = UIColor.blueColor().CGColor | |
// add it to our view | |
self.layerView.layer.addSublayer(self.colorLayer) | |
} | |
@IBAction func changeColor() { | |
// create a new randow color | |
var red:CGFloat = CGFloat(arc4random()) / CGFloat(INT_MAX) | |
var green:CGFloat = CGFloat(arc4random())/CGFloat(INT_MAX) | |
var blue:CGFloat = CGFloat(arc4random())/CGFloat(INT_MAX) | |
var color:UIColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0) | |
// create a basic animation | |
var animation:CABasicAnimation = CABasicAnimation() | |
animation.keyPath = "backgroundColor" | |
animation.toValue = color.CGColor | |
// apple animation with snap back | |
self.applyBasicAnimation(animation, layer: self.colorLayer); | |
} | |
func applyBasicAnimation(animation:CABasicAnimation, layer:CALayer) { | |
// set the from value (using presentation layer if available) | |
animation.fromValue = (layer.presentationLayer() ?? layer).valueForKeyPath(animation.keyPath) | |
// update the property in advance | |
// note: this approach will only work if toValue != nil | |
CATransaction.begin() | |
CATransaction.setDisableActions(true) | |
layer.setValue(animation.toValue, forKey: animation.keyPath) | |
CATransaction.commit() | |
// apply animation to layer | |
layer.addAnimation(animation, forKey: nil) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// animation83 | |
// | |
// Created by Sunny Cheung on 25/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet var layerView:UIView! | |
var colorLayer:CALayer! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
// create sublayer | |
self.colorLayer = CALayer() | |
self.colorLayer.frame = CGRectMake(50,50,100,100) | |
self.colorLayer.backgroundColor = UIColor.blueColor().CGColor | |
// add it to our view | |
self.layerView.layer.addSublayer(self.colorLayer) | |
} | |
@IBAction func changeColor() { | |
// create a new randow color | |
var red:CGFloat = CGFloat(arc4random()) / CGFloat(INT_MAX) | |
var green:CGFloat = CGFloat(arc4random())/CGFloat(INT_MAX) | |
var blue:CGFloat = CGFloat(arc4random())/CGFloat(INT_MAX) | |
var color:UIColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0) | |
// create a basic animation | |
var animation:CABasicAnimation = CABasicAnimation() | |
animation.keyPath = "backgroundColor" | |
animation.toValue = color.CGColor | |
animation.delegate = self | |
// apple animation with snap back | |
self.colorLayer.addAnimation(animation, forKey: nil) | |
} | |
override func animationDidStop(anim: CAAnimation, finished flag: Bool) { | |
// set the backgroundColor Property to match animation toValue | |
CATransaction.begin() | |
CATransaction.setDisableActions(true) | |
self.colorLayer.backgroundColor = (anim as CABasicAnimation).toValue as CGColor | |
CATransaction.commit() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// animation84 | |
// | |
// Created by Sunny Cheung on 25/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
import QuartzCore | |
class ViewController: UIViewController { | |
@IBOutlet var hourHand:UIImageView! | |
@IBOutlet var minuteHand:UIImageView! | |
@IBOutlet var secondHand:UIImageView! | |
var timer:NSTimer! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
// adjust anchor points | |
self.secondHand.layer.anchorPoint = CGPointMake(0.5,0.9) | |
self.minuteHand.layer.anchorPoint = CGPointMake(0.5,0.9) | |
self.hourHand.layer.anchorPoint = CGPointMake(0.5,0.9) | |
// start timer | |
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("tick"), userInfo: nil, repeats: true) | |
// set initial hand positions | |
self.updateHandsAnimated(false) | |
} | |
func tick() { | |
self.updateHandsAnimated(true) | |
} | |
func updateHandsAnimated(animated:Bool) { | |
// convert time to hours, minutes and seconds | |
var calendar:NSCalendar! = NSCalendar(calendarIdentifier: NSGregorianCalendar) | |
var units:NSCalendarUnit = .CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond | |
var components: NSDateComponents = calendar.components(units, fromDate: NSDate()) | |
// calculate hour hand angle | |
var hourAngle:CGFloat = (CGFloat(components.hour) / 12.0) * CGFloat(M_PI) * 2.0 | |
// calculate minute hand angle | |
var minuteAngle:CGFloat = (CGFloat(components.minute) / 60.0) * CGFloat(M_PI) * 2.0 | |
// calculate second hand angle | |
var secondAngle:CGFloat = (CGFloat(components.second) / 60.0) * CGFloat(M_PI) * 2.0 | |
self.setAngle(hourAngle, handView: self.hourHand, animated: animated) | |
self.setAngle(minuteAngle, handView: self.minuteHand, animated: animated) | |
self.setAngle(secondAngle, handView: self.secondHand, animated: animated) | |
} | |
func setAngle(angle:CGFloat, handView:UIView, animated:Bool) { | |
//generate transform | |
var transform:CATransform3D = CATransform3DMakeRotation(angle, 0, 0, 1) | |
if (animated) { | |
var animation:CABasicAnimation = CABasicAnimation() | |
animation.keyPath = "transform" | |
animation.toValue = NSValue(CATransform3D: transform) | |
animation.duration = 0.5 | |
animation.delegate = self | |
animation.setValue(handView, forKey:"handView") | |
handView.layer.addAnimation(animation, forKey: nil) | |
} | |
else { | |
// set transform directly | |
handView.layer.transform = transform | |
} | |
} | |
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) { | |
var handView:UIView = anim.valueForKey("handView") as UIView | |
handView.layer.transform = (anim as CABasicAnimation).toValue.CATransform3DValue | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// animation87 | |
// | |
// Created by Sunny Cheung on 26/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet var containerView:UIView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
// create a path | |
var bezierPath:UIBezierPath = UIBezierPath() | |
bezierPath.moveToPoint(CGPointMake(0,150)) | |
bezierPath.addCurveToPoint(CGPointMake(300,150), controlPoint1: CGPointMake(75,0), controlPoint2: CGPointMake(225,300)) | |
// draw the path using a CAShaperLayer | |
var pathLayer:CAShapeLayer = CAShapeLayer() | |
pathLayer.path = bezierPath.CGPath | |
pathLayer.fillColor = UIColor.clearColor().CGColor | |
pathLayer.strokeColor = UIColor.redColor().CGColor | |
pathLayer.lineWidth = 3.0 | |
self.containerView.layer.addSublayer(pathLayer) | |
// add the ship | |
var shipLayer:CALayer = CALayer() | |
shipLayer.frame = CGRectMake(0,0,64,64) | |
shipLayer.position = CGPointMake(0,150) | |
shipLayer.contents = UIImage(named: "Ship.png")!.CGImage | |
self.containerView.layer.addSublayer(shipLayer) | |
// create the keyframe animation | |
var animation:CAKeyframeAnimation = CAKeyframeAnimation() | |
animation.keyPath = "position" | |
animation.duration = 4.0 | |
animation.path = bezierPath.CGPath | |
animation.rotationMode = kCAAnimationRotateAuto | |
shipLayer.addAnimation(animation, forKey: nil) | |
} | |
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