ITP 342 Mobile App Dev. Animation

Similar documents
ITP 342 Mobile App Dev. Animation

ITP 342 Mobile App Dev. Animation

Mobile Development Lab 3

Advanced ios. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 20 11/01/2012

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?

ios Mobile Development

Praktikum Entwicklung von Mediensystemen mit

CSC 581: Mobile App Development Spring 2019

Core Graphics & Animation. ios App Development Fall 2010 Lecture 17

Cocoa Touch Best Practices

ITP 342 Mobile App Dev. Unit Testing

ITP 342 Mobile App Dev. Connections

ITP 342 Mobile App Dev. Connections

ITP 342 Mobile App Dev. Delegates

ITP 342 Mobile App Dev

Monday, 1 November The ios System

Event Delivery: The Responder Chain

Adopting Advanced Features of the New UI

SWIFT! init(title: String) { self.title = title } // required initializer w/ named parameter

COMP327 Mobile Computing Session:

Custom Drawing & Animation. CS 442: Mobile App Development Michael Saelee

Core Animation. Building Animated UI s. Bill Dudney. Gala Factory Software LLC. Bill Dudney Core Animation: Building Animated UI s Slide 1

Custom Drawing & Animation. CS 442: Mobile App Development Michael Saelee

lecture 10 UI/UX and Programmatic Design cs : spring 2018

Stanford CS193p. Developing Applications for ios. Spring Stanford CS193p. Spring 2012

ios Application Development Lecture 5: Protocols, Extensions,TabBar an Scroll Views

What's New in UIKit Dynamics and Visual Effects Session 229

ios DeCal : Lecture 2 Structure of ios Applications: MVC and Auto Layout

Enhancing your apps for the next dimension of touch

ITP 342 Mobile App Dev. Fundamentals

ITP 342 Mobile App Dev. Functions

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Stanford CS193p. Developing Applications for ios Fall Stanford CS193p. Fall 2013

Cisco StadiumVision Mobile API for Apple ios

Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

Advanced Cocoa Text Tips and Tricks. Aki Inoue Cocoa Engineer

Stanford CS193p. Developing Applications for ios. Winter CS193p! Winter 2015

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Fall Stanford CS193p Fall 2010

Apple s new Swift language

Kevin van Vechten Core OS

Finding Bugs Using Xcode Runtime Tools

Tip Calculator App Introducing Swift, Text Fields, Sliders, Outlets, Actions, View Controllers, Event Handling, NSDecimalNumber,

SWIFT - CLOSURES. Global Functions Nested Functions Closure Expressions. Have a name. Capture values from enclosing function

Announcements. Today s Topics

Stanford CS193p. Developing Applications for ios. Fall Stanford CS193p. Fall 2011

Introduction to Swift. Dr. Sarah Abraham

ITP 342 Mobile App Dev. Web View

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

View Controller Advancements for ios8

Contents. iphone Training. Industry Trainers. Classroom Training Online Training ON-DEMAND Training. Read what you need

egrapher Language Reference Manual

ITP 342 Mobile App Dev. Table Views

Science. Computer Science

ITP 342 Mobile App Dev. Data Types

Xcode & Swift: Introduction

Topics in Mobile Computing

Mobile Application Development

1 Lexical Considerations

Introductory ios Development

Lexical Considerations

CSE 438: Mobile Application Development Lab 2: Virtual Pet App

Porting Objective-C to Swift. Richard Ekle

ITP 342 Mobile App Dev. Fundamentals

CS193P - Lecture 16. iphone Application Development. Audio APIs Video Playback Displaying Web Content Settings

What s New in NSCollectionView Session 225

SWIFT BASICS

COMP327 Mobile Computing Session: Lecture Set 1a - Swift Introduction and the Foundation Framework Part 2

E Event-based parser, XML, 180 Extended attributes, URLs, 118 API, 119 command line, 118 description, 118 NSURL category, 119

My First Cocoa Program

ITP 342 Mobile App Dev. Collection View

Typescript on LLVM Language Reference Manual

Structuring an App Copyright 2013 Apple Inc. All Rights Reserved.

User Experience: Windows & Views

Media Playback and Recording. CS193W - Spring Lecture 3

Getting the Most out of Playgrounds in Xcode

Mysteries of Auto Layout, Part 1

let w = UIWindow(frame: UIScreen.mainScreen().bounds)

CS111: PROGRAMMING LANGUAGE II

CSE 230 Intermediate Programming in C and C++ Functions

Objective-C Primer. iphone Programmer s Association. Lorenzo Swank September 10, 2008

Accustoming Yourself to Objective-C

App SandBox Directory

Grand Central Dispatch

My First iphone App. 1. Tutorial Overview

CSE 341: Programming Languages

HACKING WITH SWIFT. Practical. ios 10 COMPLETE TUTORIAL COURSE. Learn to develop apps. for ios 10 by building MP. real-world projects E S

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

AT&T U-verse Enabled. How to use the JSONContainer. Publication Date: October 25th 2013

AdFalcon ios SDK Developer's Guide. AdFalcon Mobile Ad Network Product of Noqoush Mobile Media Group

Table of Contents. ios Spellbook

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Decision Making in C

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles.

Lexical Considerations

Views. A view (i.e. UIView subclass) represents a rectangular area Defines a coordinate space

Objectives. Introduce static keyword examine syntax describe common uses

Objective-C. Stanford CS193p Fall 2013

Transcription:

ITP 342 Mobile App Dev Animation

Core Animation Introduced in Mac OS X Leopard Uses animatable "layers" built on OpenGL UIKit supports Core Animation out of the box Every UIView has a CALayer behind it 2

View Animations UIKit makes basic animation easy Multiple view properties are animatable frame (position and size relative to superview) bounds (size) center (relative to superview) transform (2D scale, rotate, translate) alpha (transparency) backgroundcolor contentstretch (fill available space) 3

Animation Methods UIView has several class methods for animation: + animatewithduration: animations: + animatewithduration: animations: completion: + animatewithduration: delay: options: animations: completion: + animatewithduration: delay: usingspringwithdamping: initialspringvelocity: options: animations: completion: 4

Using Blocks to Animate UIView has various class methods that we can use to animate, but they use blocks. So we need to learn about blocks. A block is a chunk of code that can be executed at some future time. Blocks are functions that are objects. 5

Working with Blocks Blocks are a language-level feature added to C, Objective-C and C++, which allow you to create distinct segments of code that can be passed around to methods or functions as if they were values. Blocks are Objective-C objects, which means they can be added to collections like NSArray or NSDictionary. They also have the ability to capture values from the enclosing scope, making them similar to closures or lambdas in other programming languages. 6

Block Syntax The syntax to define a block literal uses the caret symbol (^), like this: ^{ NSLog(@"This is a block"); As with function and method definitions, the braces indicate the start and end of the block. In this example, the block doesn t return any value, and doesn t take any arguments. 7

Declaring a Block You use the ^ operator to declare a block variable and to indicate the beginning of a block literal. The body of the block itself is contained within {. 8

Pass Blocks as Arguments It s common to pass blocks to functions or methods for invocation elsewhere. Blocks are also used for callbacks, defining the code to be executed when a task completes. You can define the callback behavior at the time you initiate the task. Several methods in the Cocoa frameworks take a block as an argument, typically either to perform an operation on a collection of objects, or to use as a callback after an operation has finished. 9

Block Should Be Last It s best practice to use only one block argument to a method. If the method also needs other non-block arguments, the block should come last: - (void) begintaskwithname: (NSString *)name completion: (void(^)(void)) callback This makes the method call easier to read when specifying the block inline, like this: [self begintaskwithname:@"mytask" completion:^{ NSLog(@"The task is complete"); ]; 10

Back to Animation UIView has several class methods for animation: + animatewithduration: animations: + animatewithduration: animations: completion: + animatewithduration: delay: options: animations: completion: + animatewithduration: delay: usingspringwithdamping: initialspringvelocity: options: animations: completion: 11

Animate Animate changes to one or more views using the specified duration. + (void)animatewithduration: (NSTimeInterval)duration animations:(void (^)(void))animations Parameters duration: The total duration of the animations, measured in seconds. If you specify a negative value or 0, the changes are made without animating them. animations: A block object containing the changes to commit to the views. This is where you programmatically change any animatable properties of the views in your view hierarchy. This block takes no parameters and has no return value. This parameter must not be NULL. 12

Example 1 Animation In the ViewController, create a method that passes in a new name (as a NSString) and fades it in. 13

Example 1 Animation - (void) fadeinstudent:(nsstring *) name{ // Alpha = 0 means the text is transparent self.namelabel.alpha = 0; self.namelabel.text = name; [UIView animatewithduration:1.0 animations:^{ // Fade in the text of the label self.namelabel.alpha = 1; ]; SWIFT func fadeinstudent(name: String){ // Alpha = 0 means the text is transparent self.namelabel.alpha = 0 self.namelabel.text = name UIView.animate(withDuration: 1.0) { // Fade in the text of the label self.namelabel.alpha = 1 14

What we have: Animate The old string disappears and the new string fades in. What we want: We want the old string to fade out first. We need 2 animations First one to fade out old string Second one to fade in new string after the first one finishes 15

Animate Animate changes to one or more views using the specified duration and completion handler. + (void) animatewithduration: (NSTimeInterval) duration animations: (void (^)(void)) animations completion:(void (^)(BOOL finished))completion Parameters duration: The total duration of the animations, measured in seconds. animations: A block object containing the changes to commit to the views. completion: A block object to be executed when the animation sequence ends. This block has no return value and takes a single Boolean argument that indicates whether or not the animations actually finished before the completion handler was called. 16

Example 2 Animations Create a method that fades in the new text. The example in class was the animatestudent: firstname: method. Create a method that fades out the old text of the label. In the completion argument for the animation, call the method that fades in the new text. The example in class was the displaystudent: firstname: method. 17

Example 2 Animations Create a method that fades in the new text. - (void) animatestudent:(nsstring *)name{ self.namelabel.text = name; [UIView animatewithduration:1.0 animations:^{ self.namelabel.alpha = 1; ]; SWIFT func animatestudent(name: String){ self.namelabel.text = name UIView.animate(withDuration: 1.0) { self.namelabel.alpha = 1 18

Example 2 Animations Create a method that fades out the old text of the label. In the completion argument for the animation, call the method that fades in the new text. 19

Example 2 Animations - (void)displaystudent:(nsstring *)name{ [UIView animatewithduration:1.0 animations:^{ // Fade out old text of label self.namelabel.alpha = 0; completion:^(bool finished) { // Upon completion, call animatestudent [self animatestudent:name]; ]; SWIFT func displaystudent(name: String){ UIView.animate(withDuration: 1.0, animations: { // Fade out old text of label self.namelabel.alpha = 0 ) { (finished) in // Upon completion, call animatestudent self.animatestudent(name: name) 20

Example Colors Add code to rotate between 2 colors - (void) animatestudent: (NSString *)name{ self.namelabel.text = name; // If black, set to USC cardinal red if (self.namelabel.textcolor == UIColor.blackColor){ self.namelabel.textcolor = [UIColor colorwithred:153.0f/255.0f green:0.0 blue:0.0 alpha:1.0]; else{ self.namelabel.textcolor = UIColor.blackColor; [UIView animatewithduration:1.0 animations:^{ self.namelabel.alpha = 1; ]; 21

Example Colors Add code to rotate between 2 colors SWIFT func animatestudent(name: String){ self.namelabel.text = name // If black, set to USC cardinal red if self.namelabel.textcolor == UIColor.black{ self.namelabel.textcolor = UIColor(red: 153.0/255.0, green: 0.0, blue: 0.0, alpha: 1.0) else{ self.namelabel.textcolor = UIColor.black UIView.animate(withDuration: 1.0) { self.namelabel.alpha = 1.0 22

Auto Layout & Animation If you are using Auto Layout, then some attributes will not animate such position and size. To turn off Auto Layout Select 23

Resources Apple's Developer Site: https://developer.apple.com/library/ios/document ation/windowsviews/conceptual/viewpg_iphoneo s/animatingviews/animatingviews.html https://developer.apple.com/library/ios/docume NTATION/Cocoa/Conceptual/Blocks/Articles/00_I ntroduction.html ios Animation Tutorial: https://www.raywenderlich.com/113674/iosanimation-tutorial-getting-started 24