SlideShare a Scribd company logo
1 of 19
Fun with
Animation in Swift
iOS Devs NYC, 12/15/14
About me
Arun Nagarajan (@entaq)
Currently
Founding Engineer, funded stealth startup in NYC
We are hiring! Email me at arun@isapp.com
Previously
2 yrs at Google - Tech Lead, Developer Platform
9 yrs at Verivo Software (Boston) - VP of Architecture
Lets get started
● Swift
o Awesome new language with power of Cocoa Touch
● XCode 6
o Playground is brand new feature to try out code
Animation/Graphics Stack
UIView Animation calls
● Block based class methods started in iOS 4
● Waaaay cleaner than beginAnimations: and commitAnimations:
● iOS 7 added the Physics engine and Sprite Kit.
○ We’ll cover a bit of the Physics engine
UIView animatable properties
Animations in Playground
Caution: Playgrounds are pretty flaky. Might have to restart
simulator a lot :)
Run Playground as needed
Animations in Playground - code
import UIKit
import XCPlayground
let view = UIView()
//YOUR CODE HERE
XCPShowView("Container", view)
Simple property animation
let blueBox = UIView(frame: CGRect(x: 0, y: 20, width: 50, height: 50))
blueBox.backgroundColor = UIColor.blueColor()
view.addSubview(blueBox)
UIView.animateWithDuration(2, animations: {
blueBox.backgroundColor = UIColor.redColor()
blueBox.frame = CGRectMake(250, 20, 50, 100)
}, completion: { animationFinished in
}
)
Animation options
let options = UIViewAnimationOptions.Autoreverse |
UIViewAnimationOptions.Repeat |
UIViewAnimationOptions.CurveEaseInOut
UIView.animateWithDuration(2, delay: 0, options: options, animations: {
blueBox.backgroundColor = UIColor.redColor()
blueBox.frame = CGRectMake(250, 20, 50, 100)
}, completion: { animationFinished in
}
)
● Useful for specifying animation curves
● Also helpful for basic auto reversing and repetition
Completion block
● Useful for cleanup and sequencing
● Note, system animation option below. Only “Delete” is available for now
UIView.animateWithDuration(2, delay: 0, options: nil, animations: {
blueBox.backgroundColor = UIColor.redColor()
blueBox.frame = CGRectMake(250, 20, 50, 100)
}, completion: { animationFinished in
blueBox.removeFromSuperview()
//UIView.performSystemAnimation(UISystemAnimation.Delete, onViews:
[blueBox], options: nil , animations: nil, completion: nil)
}
)
Randomizing and looping
for i in 0...10 {
let blueBox = UIView(frame: CGRect(x: 0, y: 200, width: 60, height: 60))
blueBox.backgroundColor = UIColor.blueColor()
let duration = 3.0
let delay = NSTimeInterval( (Int(rand() % 900)+100) / 1000)
let options = UIViewAnimationOptions.CurveEaseInOut
let size : CGFloat = CGFloat( Int(rand()) % 40) + 20.0
let yPosition : CGFloat = CGFloat(Int(rand()) % 200) + 20.0
UIView.animateWithDuration(duration, delay: delay, options: options, animations: {
blueBox.backgroundColor = UIColor.redColor()
blueBox.frame = CGRectMake(320, yPosition, size, size)
}, completion: { animationFinished in
blueBox.removeFromSuperview()
})
view.addSubview(blueBox)
}
Views vs. Layers -
● Layers are more low level
● 3D animations need to happen here
Core Animation example
let blueBox = UIView(frame: CGRect(x: 120, y: 120, width: 50, height: 50))
blueBox.backgroundColor = UIColor.blueColor()
view.addSubview(blueBox)
UIView.animateWithDuration(2, animations: {
var anim = CABasicAnimation(keyPath: "cornerRadius")
anim.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
anim.fromValue = 0
anim.toValue = 20
anim.duration = 2
blueBox.layer.cornerRadius = 20
blueBox.layer.addAnimation(anim, forKey: nil)
}
)
Core Animation - Affine Transform
func shake(view: UIView, times: Int, direction: CGFloat, currentCount: Int, delta: CGFloat, duration:
NSTimeInterval){
UIView.animateWithDuration(duration, animations: {
view.layer.setAffineTransform(CGAffineTransformMakeTranslation(delta * direction, 0))
}, completion: { animationFinished in
if(currentCount >= times){
UIView.animateWithDuration(duration, animations:
{view.layer.setAffineTransform(CGAffineTransformIdentity)})
return
}
shake(view, times-1, direction * -1, currentCount+1, delta, duration)
})
}
var greenBox = UIView(frame:CGRect(x: 30,y: 40,width: 100,height: 100))
greenBox.backgroundColor = UIColor.greenColor()
view.addSubview(greenBox)
shake(greenBox, 10,1,0, 10,0.05)
UIKit Dynamics
var animator:UIDynamicAnimator? = nil
let gravity = UIGravityBehavior()
let collider = UICollisionBehavior()
func setupAnimator() {
let box = UIView(frame: CGRectMake(100, 100, 30, 30))
box.backgroundColor = UIColor.redColor()
view.addSubview(box)
animator = UIDynamicAnimator(referenceView:view);
gravity.addItem(box);
gravity.gravityDirection = CGVectorMake(-0.1, 0.8)
animator?.addBehavior(gravity);
collider.addItem(box)
collider.translatesReferenceBoundsIntoBoundary = true
animator?.addBehavior(collider)
}
Recap
● UIView animation
o Options, completion, fun with randomization
● Core Animation sample
o Radius, shadows etc.
o Affine Transform
● UIKit physics/dynamics
Thanks
Questions?
Arun Nagarajan
arun@isapp.com

More Related Content

What's hot

[Android] Android Animation
[Android] Android Animation[Android] Android Animation
[Android] Android AnimationNikmesoft Ltd
 
Android Training (Animation)
Android Training (Animation)Android Training (Animation)
Android Training (Animation)Khaled Anaqwa
 
Android animations
Android animationsAndroid animations
Android animationsKumar
 
Getting Started with CoreGraphics
Getting Started with CoreGraphicsGetting Started with CoreGraphics
Getting Started with CoreGraphicsXamarin
 
FMX2013: Butterfly Effect
FMX2013: Butterfly EffectFMX2013: Butterfly Effect
FMX2013: Butterfly EffectRenaldas Zioma
 
Xamarin 9/10 San Diego Meetup
Xamarin 9/10 San Diego MeetupXamarin 9/10 San Diego Meetup
Xamarin 9/10 San Diego MeetupSeamgen
 
Animações Fluídas no Android - DevFestPR 17
Animações Fluídas no Android - DevFestPR 17Animações Fluídas no Android - DevFestPR 17
Animações Fluídas no Android - DevFestPR 17Renato Peterman
 
Beginners Guide to Modeling with Maya
Beginners Guide to Modeling with MayaBeginners Guide to Modeling with Maya
Beginners Guide to Modeling with MayaPaddy Lock
 
Crafting interactions with Core Animations, David Ortinau
Crafting interactions with Core Animations, David OrtinauCrafting interactions with Core Animations, David Ortinau
Crafting interactions with Core Animations, David OrtinauXamarin
 
Animate The Web With Ember.js - Jessica Jordan
Animate The Web With Ember.js - Jessica JordanAnimate The Web With Ember.js - Jessica Jordan
Animate The Web With Ember.js - Jessica JordanJessica Jordan
 
Animations in iOS with Facebook POP
Animations in iOS with Facebook POPAnimations in iOS with Facebook POP
Animations in iOS with Facebook POPEduard Panasiuk
 
Game Engine Architecture
Game Engine ArchitectureGame Engine Architecture
Game Engine ArchitectureAttila Jenei
 
Introduction to POP animation engine
Introduction to POP animation engineIntroduction to POP animation engine
Introduction to POP animation engineSubhransu Behera
 
【Unite 2017 Tokyo】Anima2Dについて語るで!2Dアニメーションの未来
【Unite 2017 Tokyo】Anima2Dについて語るで!2Dアニメーションの未来【Unite 2017 Tokyo】Anima2Dについて語るで!2Dアニメーションの未来
【Unite 2017 Tokyo】Anima2Dについて語るで!2Dアニメーションの未来Unite2017Tokyo
 
Design your 3d game engine
Design your 3d game engineDesign your 3d game engine
Design your 3d game engineDaosheng Mu
 
ميهين
ميهينميهين
ميهينAhmed
 

What's hot (18)

Android animation theory
Android animation theoryAndroid animation theory
Android animation theory
 
[Android] Android Animation
[Android] Android Animation[Android] Android Animation
[Android] Android Animation
 
Android Training (Animation)
Android Training (Animation)Android Training (Animation)
Android Training (Animation)
 
Android animations
Android animationsAndroid animations
Android animations
 
Getting Started with CoreGraphics
Getting Started with CoreGraphicsGetting Started with CoreGraphics
Getting Started with CoreGraphics
 
Intro to auto_desk_maya2015
Intro to auto_desk_maya2015Intro to auto_desk_maya2015
Intro to auto_desk_maya2015
 
FMX2013: Butterfly Effect
FMX2013: Butterfly EffectFMX2013: Butterfly Effect
FMX2013: Butterfly Effect
 
Xamarin 9/10 San Diego Meetup
Xamarin 9/10 San Diego MeetupXamarin 9/10 San Diego Meetup
Xamarin 9/10 San Diego Meetup
 
Animações Fluídas no Android - DevFestPR 17
Animações Fluídas no Android - DevFestPR 17Animações Fluídas no Android - DevFestPR 17
Animações Fluídas no Android - DevFestPR 17
 
Beginners Guide to Modeling with Maya
Beginners Guide to Modeling with MayaBeginners Guide to Modeling with Maya
Beginners Guide to Modeling with Maya
 
Crafting interactions with Core Animations, David Ortinau
Crafting interactions with Core Animations, David OrtinauCrafting interactions with Core Animations, David Ortinau
Crafting interactions with Core Animations, David Ortinau
 
Animate The Web With Ember.js - Jessica Jordan
Animate The Web With Ember.js - Jessica JordanAnimate The Web With Ember.js - Jessica Jordan
Animate The Web With Ember.js - Jessica Jordan
 
Animations in iOS with Facebook POP
Animations in iOS with Facebook POPAnimations in iOS with Facebook POP
Animations in iOS with Facebook POP
 
Game Engine Architecture
Game Engine ArchitectureGame Engine Architecture
Game Engine Architecture
 
Introduction to POP animation engine
Introduction to POP animation engineIntroduction to POP animation engine
Introduction to POP animation engine
 
【Unite 2017 Tokyo】Anima2Dについて語るで!2Dアニメーションの未来
【Unite 2017 Tokyo】Anima2Dについて語るで!2Dアニメーションの未来【Unite 2017 Tokyo】Anima2Dについて語るで!2Dアニメーションの未来
【Unite 2017 Tokyo】Anima2Dについて語るで!2Dアニメーションの未来
 
Design your 3d game engine
Design your 3d game engineDesign your 3d game engine
Design your 3d game engine
 
ميهين
ميهينميهين
ميهين
 

Similar to Fun with Animation in Swift: UIView, Core Animation, and UIKit Dynamics

Game development via_sprite_kit
Game development via_sprite_kitGame development via_sprite_kit
Game development via_sprite_kitBuşra Deniz, CSM
 
Tiling and Zooming ASCII Art @ iOSoho
Tiling and Zooming ASCII Art @ iOSohoTiling and Zooming ASCII Art @ iOSoho
Tiling and Zooming ASCII Art @ iOSohoDaniel Doubrovkine
 
Visual Component Testing -- w/ Gil Tayar (Applitools) and Gleb Bahmutov (Cyp...
Visual Component Testing  -- w/ Gil Tayar (Applitools) and Gleb Bahmutov (Cyp...Visual Component Testing  -- w/ Gil Tayar (Applitools) and Gleb Bahmutov (Cyp...
Visual Component Testing -- w/ Gil Tayar (Applitools) and Gleb Bahmutov (Cyp...Applitools
 
Migrating Objective-C to Swift
Migrating Objective-C to SwiftMigrating Objective-C to Swift
Migrating Objective-C to SwiftElmar Kretzer
 
Om Pawar MP AJP.docx
Om Pawar MP AJP.docxOm Pawar MP AJP.docx
Om Pawar MP AJP.docxOmpawar61
 
HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?Ankara JUG
 
Augmented reality in web rtc browser
Augmented reality in web rtc browserAugmented reality in web rtc browser
Augmented reality in web rtc browserALTANAI BISHT
 
Creating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdfCreating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdfShaiAlmog1
 
Writing videogames with titanium appcelerator
Writing videogames with titanium appceleratorWriting videogames with titanium appcelerator
Writing videogames with titanium appceleratorAlessio Ricco
 
Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Jay Coskey
 
CocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIViewCocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIViewCocoaHeads France
 
I phone勉強会 (2011.11.23)
I phone勉強会 (2011.11.23)I phone勉強会 (2011.11.23)
I phone勉強会 (2011.11.23)Katsumi Kishikawa
 
Макс Грибов — Использование SpriteKit в неигровых приложениях
Макс Грибов — Использование SpriteKit в неигровых приложенияхМакс Грибов — Использование SpriteKit в неигровых приложениях
Макс Грибов — Использование SpriteKit в неигровых приложенияхCocoaHeads
 
Enhancing UI/UX using Java animations
Enhancing UI/UX using Java animationsEnhancing UI/UX using Java animations
Enhancing UI/UX using Java animationsNaman Dwivedi
 
Intro to computer vision in .net
Intro to computer vision in .netIntro to computer vision in .net
Intro to computer vision in .netStephen Lorello
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and GraphicsOXUS 20
 
[1D6]RE-view of Android L developer PRE-view
[1D6]RE-view of Android L developer PRE-view[1D6]RE-view of Android L developer PRE-view
[1D6]RE-view of Android L developer PRE-viewNAVER D2
 

Similar to Fun with Animation in Swift: UIView, Core Animation, and UIKit Dynamics (20)

Game development via_sprite_kit
Game development via_sprite_kitGame development via_sprite_kit
Game development via_sprite_kit
 
Tiling and Zooming ASCII Art @ iOSoho
Tiling and Zooming ASCII Art @ iOSohoTiling and Zooming ASCII Art @ iOSoho
Tiling and Zooming ASCII Art @ iOSoho
 
Visual Component Testing -- w/ Gil Tayar (Applitools) and Gleb Bahmutov (Cyp...
Visual Component Testing  -- w/ Gil Tayar (Applitools) and Gleb Bahmutov (Cyp...Visual Component Testing  -- w/ Gil Tayar (Applitools) and Gleb Bahmutov (Cyp...
Visual Component Testing -- w/ Gil Tayar (Applitools) and Gleb Bahmutov (Cyp...
 
Migrating Objective-C to Swift
Migrating Objective-C to SwiftMigrating Objective-C to Swift
Migrating Objective-C to Swift
 
Om Pawar MP AJP.docx
Om Pawar MP AJP.docxOm Pawar MP AJP.docx
Om Pawar MP AJP.docx
 
A More Flash Like Web?
A More Flash Like Web?A More Flash Like Web?
A More Flash Like Web?
 
HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?
 
Augmented reality in web rtc browser
Augmented reality in web rtc browserAugmented reality in web rtc browser
Augmented reality in web rtc browser
 
Creating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdfCreating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdf
 
Writing videogames with titanium appcelerator
Writing videogames with titanium appceleratorWriting videogames with titanium appcelerator
Writing videogames with titanium appcelerator
 
Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3
 
mobl
moblmobl
mobl
 
CocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIViewCocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIView
 
I phone勉強会 (2011.11.23)
I phone勉強会 (2011.11.23)I phone勉強会 (2011.11.23)
I phone勉強会 (2011.11.23)
 
Макс Грибов — Использование SpriteKit в неигровых приложениях
Макс Грибов — Использование SpriteKit в неигровых приложенияхМакс Грибов — Использование SpriteKit в неигровых приложениях
Макс Грибов — Использование SpriteKit в неигровых приложениях
 
Enhancing UI/UX using Java animations
Enhancing UI/UX using Java animationsEnhancing UI/UX using Java animations
Enhancing UI/UX using Java animations
 
Intro to computer vision in .net
Intro to computer vision in .netIntro to computer vision in .net
Intro to computer vision in .net
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
 
[1D6]RE-view of Android L developer PRE-view
[1D6]RE-view of Android L developer PRE-view[1D6]RE-view of Android L developer PRE-view
[1D6]RE-view of Android L developer PRE-view
 

More from Arun Nagarajan

Google Maps on iOS with Swift
Google Maps on iOS with SwiftGoogle Maps on iOS with Swift
Google Maps on iOS with SwiftArun Nagarajan
 
Battery Efficient Location Services
Battery Efficient Location ServicesBattery Efficient Location Services
Battery Efficient Location ServicesArun Nagarajan
 
CheckMark - Code the Deal hackathon
CheckMark - Code the Deal hackathonCheckMark - Code the Deal hackathon
CheckMark - Code the Deal hackathonArun Nagarajan
 
Mongo DB Hackday Apr 28 2012
Mongo DB Hackday Apr 28 2012Mongo DB Hackday Apr 28 2012
Mongo DB Hackday Apr 28 2012Arun Nagarajan
 
Integrate Google Drive with Google Apps Script
Integrate Google Drive with Google Apps ScriptIntegrate Google Drive with Google Apps Script
Integrate Google Drive with Google Apps ScriptArun Nagarajan
 
Android L Preview - Recents Screen + API
Android L Preview  - Recents Screen + APIAndroid L Preview  - Recents Screen + API
Android L Preview - Recents Screen + APIArun Nagarajan
 
node.js on Google Compute Engine
node.js on Google Compute Enginenode.js on Google Compute Engine
node.js on Google Compute EngineArun Nagarajan
 

More from Arun Nagarajan (8)

Google Maps on iOS with Swift
Google Maps on iOS with SwiftGoogle Maps on iOS with Swift
Google Maps on iOS with Swift
 
Battery Efficient Location Services
Battery Efficient Location ServicesBattery Efficient Location Services
Battery Efficient Location Services
 
CheckMark - Code the Deal hackathon
CheckMark - Code the Deal hackathonCheckMark - Code the Deal hackathon
CheckMark - Code the Deal hackathon
 
Mongo DB Hackday Apr 28 2012
Mongo DB Hackday Apr 28 2012Mongo DB Hackday Apr 28 2012
Mongo DB Hackday Apr 28 2012
 
Integrate Google Drive with Google Apps Script
Integrate Google Drive with Google Apps ScriptIntegrate Google Drive with Google Apps Script
Integrate Google Drive with Google Apps Script
 
Android L Preview - Recents Screen + API
Android L Preview  - Recents Screen + APIAndroid L Preview  - Recents Screen + API
Android L Preview - Recents Screen + API
 
CloudKit
CloudKitCloudKit
CloudKit
 
node.js on Google Compute Engine
node.js on Google Compute Enginenode.js on Google Compute Engine
node.js on Google Compute Engine
 

Recently uploaded

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

Fun with Animation in Swift: UIView, Core Animation, and UIKit Dynamics

  • 1. Fun with Animation in Swift iOS Devs NYC, 12/15/14
  • 2. About me Arun Nagarajan (@entaq) Currently Founding Engineer, funded stealth startup in NYC We are hiring! Email me at arun@isapp.com Previously 2 yrs at Google - Tech Lead, Developer Platform 9 yrs at Verivo Software (Boston) - VP of Architecture
  • 3. Lets get started ● Swift o Awesome new language with power of Cocoa Touch ● XCode 6 o Playground is brand new feature to try out code
  • 5. UIView Animation calls ● Block based class methods started in iOS 4 ● Waaaay cleaner than beginAnimations: and commitAnimations: ● iOS 7 added the Physics engine and Sprite Kit. ○ We’ll cover a bit of the Physics engine
  • 7. Animations in Playground Caution: Playgrounds are pretty flaky. Might have to restart simulator a lot :)
  • 9. Animations in Playground - code import UIKit import XCPlayground let view = UIView() //YOUR CODE HERE XCPShowView("Container", view)
  • 10. Simple property animation let blueBox = UIView(frame: CGRect(x: 0, y: 20, width: 50, height: 50)) blueBox.backgroundColor = UIColor.blueColor() view.addSubview(blueBox) UIView.animateWithDuration(2, animations: { blueBox.backgroundColor = UIColor.redColor() blueBox.frame = CGRectMake(250, 20, 50, 100) }, completion: { animationFinished in } )
  • 11. Animation options let options = UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.Repeat | UIViewAnimationOptions.CurveEaseInOut UIView.animateWithDuration(2, delay: 0, options: options, animations: { blueBox.backgroundColor = UIColor.redColor() blueBox.frame = CGRectMake(250, 20, 50, 100) }, completion: { animationFinished in } ) ● Useful for specifying animation curves ● Also helpful for basic auto reversing and repetition
  • 12. Completion block ● Useful for cleanup and sequencing ● Note, system animation option below. Only “Delete” is available for now UIView.animateWithDuration(2, delay: 0, options: nil, animations: { blueBox.backgroundColor = UIColor.redColor() blueBox.frame = CGRectMake(250, 20, 50, 100) }, completion: { animationFinished in blueBox.removeFromSuperview() //UIView.performSystemAnimation(UISystemAnimation.Delete, onViews: [blueBox], options: nil , animations: nil, completion: nil) } )
  • 13. Randomizing and looping for i in 0...10 { let blueBox = UIView(frame: CGRect(x: 0, y: 200, width: 60, height: 60)) blueBox.backgroundColor = UIColor.blueColor() let duration = 3.0 let delay = NSTimeInterval( (Int(rand() % 900)+100) / 1000) let options = UIViewAnimationOptions.CurveEaseInOut let size : CGFloat = CGFloat( Int(rand()) % 40) + 20.0 let yPosition : CGFloat = CGFloat(Int(rand()) % 200) + 20.0 UIView.animateWithDuration(duration, delay: delay, options: options, animations: { blueBox.backgroundColor = UIColor.redColor() blueBox.frame = CGRectMake(320, yPosition, size, size) }, completion: { animationFinished in blueBox.removeFromSuperview() }) view.addSubview(blueBox) }
  • 14. Views vs. Layers - ● Layers are more low level ● 3D animations need to happen here
  • 15. Core Animation example let blueBox = UIView(frame: CGRect(x: 120, y: 120, width: 50, height: 50)) blueBox.backgroundColor = UIColor.blueColor() view.addSubview(blueBox) UIView.animateWithDuration(2, animations: { var anim = CABasicAnimation(keyPath: "cornerRadius") anim.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) anim.fromValue = 0 anim.toValue = 20 anim.duration = 2 blueBox.layer.cornerRadius = 20 blueBox.layer.addAnimation(anim, forKey: nil) } )
  • 16. Core Animation - Affine Transform func shake(view: UIView, times: Int, direction: CGFloat, currentCount: Int, delta: CGFloat, duration: NSTimeInterval){ UIView.animateWithDuration(duration, animations: { view.layer.setAffineTransform(CGAffineTransformMakeTranslation(delta * direction, 0)) }, completion: { animationFinished in if(currentCount >= times){ UIView.animateWithDuration(duration, animations: {view.layer.setAffineTransform(CGAffineTransformIdentity)}) return } shake(view, times-1, direction * -1, currentCount+1, delta, duration) }) } var greenBox = UIView(frame:CGRect(x: 30,y: 40,width: 100,height: 100)) greenBox.backgroundColor = UIColor.greenColor() view.addSubview(greenBox) shake(greenBox, 10,1,0, 10,0.05)
  • 17. UIKit Dynamics var animator:UIDynamicAnimator? = nil let gravity = UIGravityBehavior() let collider = UICollisionBehavior() func setupAnimator() { let box = UIView(frame: CGRectMake(100, 100, 30, 30)) box.backgroundColor = UIColor.redColor() view.addSubview(box) animator = UIDynamicAnimator(referenceView:view); gravity.addItem(box); gravity.gravityDirection = CGVectorMake(-0.1, 0.8) animator?.addBehavior(gravity); collider.addItem(box) collider.translatesReferenceBoundsIntoBoundary = true animator?.addBehavior(collider) }
  • 18. Recap ● UIView animation o Options, completion, fun with randomization ● Core Animation sample o Radius, shadows etc. o Affine Transform ● UIKit physics/dynamics