Last active
February 13, 2016 17:51
-
-
Save sunnycyk/75fa08257541567b9371 to your computer and use it in GitHub Desktop.
Core Animation Example
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 | |
// animation613 | |
// | |
// Created by Sunny Cheung on 23/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 particle emitter layer | |
var emitter:CAEmitterLayer = CAEmitterLayer() | |
emitter.frame = self.containerView!.bounds | |
self.containerView!.layer.addSublayer(emitter) | |
// configure emitter | |
emitter.renderMode = kCAEmitterLayerAdditive | |
emitter.emitterPosition = CGPointMake(emitter.frame.size.width / 2.0, emitter.frame.size.height / 2.0) | |
// create a particle template | |
var cell:CAEmitterCell = CAEmitterCell() | |
cell.contents = UIImage(named: "Spark.png")!.CGImage | |
cell.birthRate = 150 | |
cell.lifetime = 5.0 | |
cell.color = UIColor(red: 1, green: 0.5, blue: 0.1, alpha: 1.0).CGColor | |
cell.alphaSpeed = -0.4 | |
cell.velocity = 50 | |
cell.velocityRange = 50 | |
cell.emissionRange = CGFloat(M_PI * 2.0) | |
emitter.emitterCells = [cell] | |
} | |
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 | |
// animation614 | |
// | |
// Created by Sunny Cheung on 23/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
import QuartzCore | |
import GLKit | |
import OpenGLES | |
class ViewController: UIViewController { | |
@IBOutlet var glView:UIView! | |
var glContext:EAGLContext! | |
var glLayer:CAEAGLLayer! | |
var framebuffer:GLuint = GLuint() | |
var colorRenderbuffer:GLuint = GLuint() | |
var framebufferWidth:GLint = GLint() | |
var framebufferHeight:GLint = GLint() | |
var effect:GLKBaseEffect! | |
func setUpBuffers() { | |
// setup frame buffer | |
glGenFramebuffers(1, &self.framebuffer) | |
glBindFramebuffer(GLuint(GL_FRAMEBUFFER),self.framebuffer) | |
// setup color render buffer | |
glGenRenderbuffers(1, &self.colorRenderbuffer) | |
glBindRenderbuffer(GLuint(GL_RENDERBUFFER), self.colorRenderbuffer) | |
glFramebufferRenderbuffer(GLuint(GL_FRAMEBUFFER), | |
GLuint(GL_COLOR_ATTACHMENT0), | |
GLuint(GL_RENDERBUFFER), | |
self.colorRenderbuffer) | |
self.glContext.renderbufferStorage(Int(GL_RENDERBUFFER), fromDrawable: self.glLayer!) | |
glGetRenderbufferParameteriv(GLuint(GL_RENDERBUFFER), GLuint(GL_RENDERBUFFER_WIDTH), &self.framebufferWidth) | |
glGetRenderbufferParameteriv(GLuint(GL_RENDERBUFFER), GLuint(GL_RENDERBUFFER_HEIGHT), &self.framebufferHeight) | |
if (glCheckFramebufferStatus(GLuint(GL_FRAMEBUFFER)) != GLuint(GL_FRAMEBUFFER_COMPLETE)) { | |
NSLog("Fail to make framebuffer object %i", glCheckFramebufferStatus(GLuint(GL_FRAMEBUFFER))) | |
} | |
} | |
func tearDownBuffers() { | |
if (self.framebuffer == 0) { | |
glDeleteBuffers(1, &self.framebuffer) | |
self.framebuffer = 0 | |
} | |
if (self.colorRenderbuffer == 0) { | |
glDeleteBuffers(1, &self.colorRenderbuffer) | |
self.colorRenderbuffer = 0 | |
} | |
} | |
func drawFrame() { | |
glBindFramebuffer(GLuint(GL_FRAMEBUFFER), self.framebuffer) | |
glViewport(0, 0, self.framebufferWidth, self.framebufferHeight) | |
// bind shader program | |
self.effect.prepareToDraw() | |
// clear the screen | |
glClear(GLuint(GL_COLOR_BUFFER_BIT)) | |
glClearColor(0, 0, 0, 1.0) | |
// setup vertices | |
var vertices:[GLfloat] = [ | |
-0.5, -0.5, -1.0, | |
0.0, 0.5, -1.0, | |
0.5, -0.5, -1.0 | |
] | |
// setup colors | |
var colors:[GLfloat] = [ | |
0.0, 0.0, 1.0, 1.0, | |
0.0, 1.0, 0.0, 1.0, | |
1.0,0.0,0.0,1.0 | |
] | |
glEnableVertexAttribArray(GLuint(GLKVertexAttrib.Position.rawValue)) | |
glEnableVertexAttribArray(GLuint(GLKVertexAttrib.Color.rawValue)) | |
glVertexAttribPointer(GLuint(GLKVertexAttrib.Position.rawValue), 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), 0, vertices) | |
glVertexAttribPointer(GLuint(GLKVertexAttrib.Color.rawValue), 4, GLenum(GL_FLOAT), GLboolean(GL_FALSE), 0, colors) | |
glDrawArrays(GLuint(GL_TRIANGLES), 0, 3) | |
glBindRenderbuffer(GLuint(GL_RENDERBUFFER), self.colorRenderbuffer) | |
self.glContext.presentRenderbuffer(Int(GL_RENDERBUFFER)) | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
// set up context | |
self.glContext = EAGLContext(API: EAGLRenderingAPI.OpenGLES2) | |
EAGLContext.setCurrentContext(self.glContext) | |
self.glLayer = CAEAGLLayer() | |
self.glLayer.frame = self.glView.bounds | |
self.glView.layer.addSublayer(self.glLayer) | |
self.glLayer.drawableProperties = [kEAGLDrawablePropertyRetainedBacking: false, | |
kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8] | |
self.effect=GLKBaseEffect() | |
self.setUpBuffers() | |
self.drawFrame() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
override func viewDidDisappear(animated: Bool) { | |
self.tearDownBuffers() | |
super.viewDidDisappear(animated) | |
} | |
} |
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 | |
// animation61 | |
// | |
// Created by Sunny Cheung on 16/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
import QuartzCore | |
class ViewController: UIViewController { | |
@IBOutlet var containerView:UIView? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
var path:UIBezierPath = UIBezierPath() | |
path.moveToPoint(CGPointMake(175,100)) | |
path.addArcWithCenter(CGPointMake(150,100), radius: 25, startAngle: 0, endAngle: CGFloat(2*M_PI), clockwise: true) | |
path.moveToPoint(CGPointMake(150,125)) | |
path.addLineToPoint(CGPointMake(150,175)) | |
path.addLineToPoint(CGPointMake(125,225)) | |
path.moveToPoint(CGPointMake(150,175)) | |
path.addLineToPoint(CGPointMake(175,225)) | |
path.moveToPoint(CGPointMake(100,150)) | |
path.addLineToPoint(CGPointMake(200, 150)) | |
var shapeLayer:CAShapeLayer = CAShapeLayer() | |
shapeLayer.strokeColor = UIColor.redColor().CGColor | |
shapeLayer.fillColor = UIColor.clearColor().CGColor | |
shapeLayer.lineWidth = 5 | |
shapeLayer.lineJoin = kCALineJoinRound | |
shapeLayer.lineCap = kCALineCapRound | |
shapeLayer.path = path.CGPath | |
self.containerView?.layer.addSublayer(shapeLayer) | |
// 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
// | |
// ScrollView.swift | |
// animation610 | |
// | |
// Created by Sunny Cheung on 23/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import Foundation | |
import QuartzCore | |
import UIKit | |
class ScrollView : UIView { | |
class override func layerClass() -> AnyClass { | |
return CAScrollLayer.self | |
} | |
func setup() { | |
// enable clipping | |
self.layer.masksToBounds = true | |
// attach pan gesture recongnizer | |
var recognizer:UIPanGestureRecognizer? | |
recognizer = UIPanGestureRecognizer(target: self, action: Selector("pan:")) | |
self.addGestureRecognizer(recognizer!) | |
} | |
override init(frame:CGRect) { | |
super.init(frame: frame) | |
self.setup() | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder:aDecoder) | |
} | |
override func awakeFromNib() { | |
self.setup() | |
} | |
func pan(recognizer:UIPanGestureRecognizer) { | |
var offset:CGPoint = self.bounds.origin | |
offset.x -= recognizer.translationInView(self).x | |
offset.y -= recognizer.translationInView(self).y | |
(self.layer as CAScrollLayer).scrollToPoint(offset) | |
recognizer.setTranslation(CGPointZero, inView: self) | |
} | |
} |
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 | |
// animation612 | |
// | |
// Created by Sunny Cheung on 23/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
import QuartzCore | |
import Foundation | |
class ViewController: UIViewController { | |
@IBOutlet var scrollView:UIScrollView? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
// add the tiled layer | |
var tileLayer:CATiledLayer = CATiledLayer() | |
tileLayer.frame = CGRectMake(0,0,2048,2048) | |
tileLayer.delegate = self | |
self.scrollView!.layer.addSublayer(tileLayer) | |
// configure the scroll view | |
self.scrollView!.contentSize = tileLayer.frame.size | |
// draw layer | |
tileLayer.setNeedsDisplay() | |
} | |
override func drawLayer(layer: CALayer!, inContext ctx: CGContext!) { | |
// determine tile coordinate | |
var bounds:CGRect = CGContextGetClipBoundingBox(ctx) | |
var x:Int = Int(bounds.origin.x / (layer as CATiledLayer).tileSize.width) | |
var y:Int = Int(bounds.origin.y / (layer as CATiledLayer).tileSize.height) | |
//load tile image | |
var imageName:NSString = NSString(format: "Snowman_%02i_%02i", x, y) | |
var imagePath:NSString = NSBundle.mainBundle().pathForResource(imageName, ofType: "jpg")! | |
var tileImage:UIImage = UIImage(contentsOfFile: imagePath)! | |
//draw title | |
UIGraphicsPushContext(ctx) | |
tileImage.drawInRect(bounds) | |
UIGraphicsPopContext() | |
} | |
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 | |
// animation616 | |
// | |
// Created by Sunny Cheung on 24/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
import AVFoundation | |
class ViewController: UIViewController { | |
@IBOutlet var containerView:UIView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
// get video URL | |
var URL:NSURL! = NSBundle.mainBundle().URLForResource("Ship", withExtension: "mp4") | |
// create player and player layer | |
var player:AVPlayer = AVPlayer(URL: URL) | |
var playerLayer:AVPlayerLayer = AVPlayerLayer(player: player) | |
// set player layer frame and attach it to our view | |
playerLayer.frame = self.containerView.bounds | |
self.containerView.layer.addSublayer(playerLayer) | |
// play the video | |
player.play() | |
} | |
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 | |
// animation616 | |
// | |
// Created by Sunny Cheung on 24/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
import AVFoundation | |
class ViewController: UIViewController { | |
@IBOutlet var containerView:UIView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
// get video URL | |
var URL:NSURL! = NSBundle.mainBundle().URLForResource("Ship", withExtension: "mp4") | |
// create player and player layer | |
var player:AVPlayer = AVPlayer(URL: URL) | |
var playerLayer:AVPlayerLayer = AVPlayerLayer(player: player) | |
// set player layer frame and attach it to our view | |
playerLayer.frame = self.containerView.bounds | |
self.containerView.layer.addSublayer(playerLayer) | |
// transform layer | |
var transform:CATransform3D = CATransform3DIdentity | |
transform.m34 = -1.0/500.0 | |
transform = CATransform3DRotate(transform, CGFloat(M_PI_4), 1, 1, 0) | |
playerLayer.transform = transform | |
// add rounded corners and border | |
playerLayer.masksToBounds = true | |
playerLayer.cornerRadius = 20.0 | |
playerLayer.borderColor = UIColor.redColor().CGColor | |
playerLayer.borderWidth = 5.0 | |
// play the video | |
player.play() | |
} | |
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 | |
// animation62 | |
// | |
// Created by Sunny Cheung on 16/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet var labelView:UIView? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
// create a text layer | |
var textLayer:CATextLayer = CATextLayer() | |
textLayer.frame = self.labelView!.bounds | |
self.labelView?.layer.addSublayer(textLayer) | |
//set text attributes | |
textLayer.foregroundColor = UIColor.blackColor().CGColor | |
textLayer.alignmentMode = kCAAlignmentJustified | |
textLayer.wrapped = true | |
// choose a font | |
var font:UIFont = UIFont.systemFontOfSize(15) | |
// set layer font | |
var fontName:CFString = CFStringCreateWithCString(nil, font.fontName, kCFStringEncodingASCII) | |
var fontRef:CGFontRef = CGFontCreateWithFontName(fontName) | |
textLayer.font = fontRef | |
textLayer.fontSize = font.pointSize | |
let text:NSString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus commodo tellus non leo laoreet convallis. Ut molestie elit vel diam luctus vestibulum. Mauris a condimentum libero. Donec pretium turpis sed porttitor tincidunt. Sed a aliquam neque. Phasellus fermentum auctor aliquam. Cras cursus tincidunt augue ac pharetra. Curabitur facilisis posuere nisl et ultricies. Praesent consequat libero nulla, eget gravida dui gravida non. Vivamus varius ornare auctor." | |
textLayer.string = text | |
} | |
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 | |
// animation63 | |
// | |
// Created by Sunny Cheung on 16/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
import QuartzCore | |
import CoreText | |
class ViewController: UIViewController { | |
@IBOutlet var labelView:UIView? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
var textLayer:CATextLayer = CATextLayer() | |
textLayer.frame = self.labelView!.bounds | |
textLayer.contentsScale = UIScreen.mainScreen().scale | |
self.labelView?.layer.addSublayer(textLayer) | |
// set Text attribute | |
textLayer.alignmentMode = kCAAlignmentJustified | |
textLayer.wrapped = true | |
// choose font | |
var font:UIFont = UIFont.systemFontOfSize(15) | |
// choose some text | |
var text:NSString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus commodo tellus non leo laoreet convallis. Ut molestie elit vel diam luctus vestibulum. Mauris a condimentum libero. Donec pretium turpis sed porttitor tincidunt. Sed a aliquam neque. Phasellus fermentum auctor aliquam. Cras cursus tincidunt augue ac pharetra. Curabitur facilisis posuere nisl et ultricies. Praesent consequat libero nulla, eget gravida dui gravida non. Vivamus varius ornare auctor." | |
// create attribute string | |
var string:NSMutableAttributedString = NSMutableAttributedString(string: text) | |
// convert UIFont to CTFont | |
var fontName:CFString = CFStringCreateWithCString(nil, font.fontName, CFStringBuiltInEncodings.UTF8.toRaw()) | |
var fontSize:CGFloat = font.pointSize | |
var fontRef = CTFontCreateWithName(fontName,fontSize,nil) | |
// set text attribute | |
var attribs:NSDictionary = [kCTForegroundColorAttributeName : UIColor.blackColor().CGColor, | |
kCTFontAttributeName: fontRef] | |
string.setAttributes(attribs,range: NSMakeRange(0, text.length)) | |
attribs = [ kCTForegroundColorAttributeName : UIColor.redColor().CGColor, | |
kCTUnderlineStyleAttributeName: NSInteger(CTUnderlineStyle.Single.toRaw()), // CTUnderlineStyle.Single cannot conform as AnyObject, ok to convert to NSInteger? | |
kCTFontAttributeName: fontRef ] | |
CTUnderlineStyle.Single | |
string.setAttributes(attribs,range: NSMakeRange(6, 5)) | |
textLayer.string = string | |
} | |
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
// | |
// LayerLabel.swift | |
// animation64 | |
// | |
// Created by Sunny Cheung on 21/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import QuartzCore | |
class LayerLabel : UILabel { | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder:aDecoder) | |
self.setup() | |
} | |
override init(frame:CGRect) { | |
super.init(frame:frame) | |
self.setup() | |
} | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
self.setup() | |
} | |
override class func layerClass() -> AnyClass { | |
return CATextLayer.self | |
} | |
func textLayer() -> CATextLayer { | |
return self.layer as CATextLayer | |
} | |
func setup() { | |
self.text = self.text | |
self.textColor = self.textColor | |
self.font = self.font | |
self.textLayer().alignmentMode = kCAAlignmentJustified | |
self.textLayer().wrapped = true | |
self.layer.display() | |
} | |
func setText(text:NSString) { | |
super.text = text | |
self.textLayer().string = text | |
} | |
func setTextColor(textColor:UIColor) { | |
super.textColor = textColor | |
self.textLayer().foregroundColor = textColor.CGColor | |
} | |
func setFont(font:UIFont) { | |
super.font = font | |
var fontName:CFString = CFStringCreateWithCString(nil,font.fontName,CFStringBuiltInEncodings.UTF8.toRaw()) | |
var fontRef:CGFontRef = CGFontCreateWithFontName(fontName) | |
self.textLayer().font = fontRef | |
self.textLayer().fontSize = font.pointSize | |
} | |
} |
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 | |
// animation65 | |
// | |
// Created by Sunny Cheung on 21/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet var containerView:UIView? | |
func faceWithTransform(transform:CATransform3D) -> CALayer { | |
var face = CALayer() | |
face.frame = CGRectMake(-50,-50,100,100) | |
var red:CGFloat = CGFloat(Double(rand()) / Double(Int32.max)) | |
var green:CGFloat = CGFloat(Double(rand()) / Double(Int32.max)) | |
var blue:CGFloat = CGFloat(Double(rand()) / Double(Int32.max)) | |
face.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0).CGColor | |
face.transform = transform | |
return face | |
} | |
func cubeWithTransform(transform:CATransform3D) -> CALayer { | |
var cube = CATransformLayer() | |
var ct:CATransform3D = CATransform3DMakeTranslation(0, 0, 50) | |
cube.addSublayer(self.faceWithTransform(ct)) | |
ct = CATransform3DMakeTranslation(50, 0, 0) | |
ct = CATransform3DRotate(ct, CGFloat(M_PI_2), 0, 1, 0) | |
cube.addSublayer(self.faceWithTransform(ct)) | |
ct = CATransform3DMakeTranslation(0, -50, 0) | |
ct = CATransform3DRotate(ct, CGFloat(M_PI_2), 1, 0, 0) | |
cube.addSublayer(self.faceWithTransform(ct)) | |
ct = CATransform3DMakeTranslation(0, 50, 0) | |
ct = CATransform3DRotate(ct, CGFloat(-M_PI_2), 1, 0, 0) | |
cube.addSublayer(self.faceWithTransform(ct)) | |
ct = CATransform3DMakeTranslation(-50, 0, 0) | |
ct = CATransform3DRotate(ct, CGFloat(-M_PI_2), 0, 1, 0) | |
cube.addSublayer(self.faceWithTransform(ct)) | |
ct = CATransform3DMakeTranslation(0, 0, -50) | |
ct = CATransform3DRotate(ct, CGFloat(M_PI), 0, 1, 0) | |
cube.addSublayer(self.faceWithTransform(ct)) | |
var containerSize = self.containerView?.bounds.size | |
cube.position = CGPointMake(containerSize!.width/2.0,containerSize!.height/2.0) | |
cube.transform = transform | |
return cube | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
var pt:CATransform3D = CATransform3DIdentity; | |
pt.m34 = -1.0/500.0 | |
self.containerView?.layer.sublayerTransform = pt | |
var clt:CATransform3D = CATransform3DIdentity | |
clt = CATransform3DTranslate(clt, -100, 0, 0) | |
var cube1:CALayer = self.cubeWithTransform(clt) | |
self.containerView!.layer.addSublayer(cube1) | |
var c2t:CATransform3D = CATransform3DIdentity | |
c2t = CATransform3DTranslate(c2t, 100, 0, 0) | |
c2t = CATransform3DRotate(c2t, CGFloat(-M_PI_4), 1, 0, 0) | |
c2t = CATransform3DRotate(c2t, CGFloat(-M_PI_4), 0, 1, 0) | |
var cube2:CALayer = self.cubeWithTransform(c2t) | |
self.containerView!.layer.addSublayer(cube2) | |
} | |
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 | |
// animation66 | |
// | |
// Created by Sunny Cheung on 22/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. | |
var gradientLayer:CAGradientLayer=CAGradientLayer() | |
gradientLayer.frame = self.containerView!.bounds | |
self.containerView?.layer.addSublayer(gradientLayer) | |
gradientLayer.colors = [UIColor.redColor().CGColor as AnyObject, UIColor.blueColor().CGColor as AnyObject] | |
gradientLayer.startPoint = CGPointMake(0,0) | |
gradientLayer.endPoint = CGPointMake(1,1) | |
} | |
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 | |
// animation67 | |
// | |
// Created by Sunny Cheung on 22/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. | |
var gradientLayer:CAGradientLayer = CAGradientLayer() | |
gradientLayer.frame = self.containerView!.bounds | |
self.containerView?.layer.addSublayer(gradientLayer) | |
gradientLayer.colors = [ UIColor.redColor().CGColor as AnyObject, | |
UIColor.yellowColor().CGColor as AnyObject, | |
UIColor.greenColor().CGColor as AnyObject] | |
gradientLayer.locations=[0.0,0.25,0.5] | |
gradientLayer.startPoint = CGPointMake(0,0) | |
gradientLayer.endPoint = CGPointMake(1,1) | |
} | |
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 | |
// animation68 | |
// | |
// Created by Sunny Cheung on 22/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. | |
var replicator:CAReplicatorLayer = CAReplicatorLayer() | |
replicator.frame = self.containerView!.bounds | |
self.containerView?.layer.addSublayer(replicator) | |
replicator.instanceCount = 10 | |
var transform:CATransform3D = CATransform3DIdentity | |
transform = CATransform3DTranslate(transform, 0, 200, 0) | |
transform = CATransform3DRotate(transform, CGFloat(Float(M_PI)/5.0), 0, 0, 1) | |
transform = CATransform3DTranslate(transform, 0, -200, 0) | |
replicator.instanceTransform = transform | |
replicator.instanceBlueOffset = -0.1 | |
replicator.instanceGreenOffset = -0.1 | |
var layer:CALayer = CALayer() | |
layer.frame = CGRectMake(100, 100, 100, 100) | |
layer.backgroundColor = UIColor.whiteColor().CGColor | |
replicator.addSublayer(layer) | |
} | |
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
// | |
// ReflectionView.swift | |
// animation69 | |
// | |
// Created by Sunny Cheung on 22/10/14. | |
// Copyright (c) 2014 khl. All rights reserved. | |
// | |
import Foundation | |
import QuartzCore | |
import UIKit | |
class ReflectionView : UIView { | |
class override func layerClass() -> AnyClass { | |
return CAReplicatorLayer.self | |
} | |
func setup() { | |
var layer:CAReplicatorLayer = self.layer as CAReplicatorLayer | |
layer.instanceCount = 2 | |
var transform:CATransform3D = CATransform3DIdentity | |
var verticalOffset:CGFloat = self.bounds.size.height+2 | |
transform = CATransform3DTranslate(transform, 0, verticalOffset, 0) | |
transform = CATransform3DScale(transform, 1, -1, 0) | |
layer.instanceTransform = transform | |
layer.instanceAlphaOffset = -0.6 | |
// layer.backgroundColor = UIColor.grayColor().CGColor | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
self.setup() | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder:aDecoder) | |
self.setup() | |
} | |
override func awakeFromNib() { | |
self.setup() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment