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

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 

Recently uploaded (20)

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 

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