Skip to content

Instantly share code, notes, and snippets.

@sunnycyk
Last active August 29, 2015 14:08
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 sunnycyk/2bb06ea4ff941669e83b to your computer and use it in GitHub Desktop.
Save sunnycyk/2bb06ea4ff941669e83b to your computer and use it in GitHub Desktop.
IOS Core Animation Example Chapter 7
//
// ViewController.swift
// animation76
//
// Created by Sunny Cheung on 25/10/14.
// Copyright (c) 2014 khl. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var layerView:UIView!
@IBOutlet 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.0, 50.0, 100.0, 100.0)
self.colorLayer.backgroundColor = UIColor.blueColor().CGColor
// add a custom action
var transition:CATransition = CATransition()
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
self.colorLayer.actions = ["backgroundColor" : transition]
// add it to our view
self.layerView.layer.addSublayer(self.colorLayer)
}
@IBAction func changeColor() {
// 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.colorLayer.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0).CGColor
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//
// ViewController.swift
// animation77
//
// Created by Sunny Cheung on 25/10/14.
// Copyright (c) 2014 khl. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var colorLayer:CALayer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.colorLayer = CALayer()
self.colorLayer.frame = CGRectMake(0, 0, 100, 100)
self.colorLayer.position = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2)
self.colorLayer.backgroundColor = UIColor.redColor().CGColor
self.view.layer.addSublayer(self.colorLayer)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
// get the touch point
var point:CGPoint = touches.anyObject()!.locationInView(self.view)
// check if we've tapped the moving layer
if (self.colorLayer.presentationLayer().hitTest(point) != nil) {
// 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.colorLayer.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0).CGColor
}
else {
CATransaction.begin()
CATransaction.setAnimationDuration(4.0)
self.colorLayer.position = point
CATransaction.commit()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//
// ViewController.swift
// animation71
//
// Created by Sunny Cheung on 24/10/14.
// Copyright (c) 2014 khl. All rights reserved.
//
import UIKit
import QuartzCore
class ViewController: UIViewController {
@IBOutlet var layerView:UIView!
@IBOutlet 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.0, 50.0, 100.0, 100.0)
self.colorLayer.backgroundColor = UIColor.blueColor().CGColor
self.layerView.layer.addSublayer(self.colorLayer)
}
@IBAction func changeColor() {
CATransaction.begin()
CATransaction.setAnimationDuration(1.0)
CATransaction.setCompletionBlock({
var transform:CGAffineTransform = self.colorLayer.affineTransform()
transform = CGAffineTransformRotate(transform, CGFloat(M_PI_4))
self.colorLayer.setAffineTransform(transform)
})
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.colorLayer.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0).CGColor
CATransaction.commit()
}
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