Thursday, April 21, 2011

Programming with Core Animation on Mac

Learning notes of Core Animation for Mac OS X and the iPhone

1. The Simplest animation (CABasicAnimation)

Without animation: [theView setFrame:newFrame];

With animation: [[theView animator] setFrame:newFrame];

[theView animator] is the Animator Proxy which is simply finding an animation and then invoking it. The default animation is CABasicAnimation.

2. CAKeyframeAnimation

With CAfeyFrameAnimation, you can define a series of key frames. CoreAnimation system will create animation based on these key frames. For example, we want to create an animation of moving an image to point A, and then B. We only need to create the key frames of A and B. CAKeyframeAnimation could be understood as a series of CABasicAnimation.

// Create the path

NSRect frame = [theView frame];

CGMutablePathRef thePath = CGPathCreateMutable();

CGPathMoveToPoint(thePath, NULL, NSMinX(frame), NSMinY(frame)); //the origin

CGPathAddLineToPoint(thePath, NULL, pointA.x, pointA.y); //Point A

CGPathAddLineToPoint(thePath, NULL, pointB.x, pointB.y); //Point B

// Create an animation

CAKeyframeAnimation *originAnimation = [CAKeyframeAnimation animation];

originAnimation.path = thePath;

originAnimation.duration = 2.0f;

originAnimation.calculationMode = kCAAnimationPaced;

// Add this animation

[theView setAnimations:[NSDictionary dictionaryWithObjectsAndKeys: originAnimation, @”frameOrigin”, nil]];

// Activate this animation

[[theView animator] setFrameOrigin:pointB]; //set the point of the last frame

Monday, April 11, 2011

Add an existing framework in Xcode 4

Steps to add an existing framework in Xcode 4:

1. Click the project in the project navigator.

2. In the project editor, click a target.

3. Select 'Build Phases' tab

4. Expand 'Link Binary With Libraries'

5. Click '+' button to add a framework.

6. Go back to the project navigator, you will see the new framework. You can drag it to 'Frameworks' group.

Friday, April 08, 2011

WWDC2010 Session211 Simplifying iPhone App Development with GCD

GCD Overview

1. GCD is part of libSystem.dylib

2. Available to all Apps.

- #include <dispatch/dispatch.h>

3. GCD API has block-based and function-based variants

- Focus today on block-based API

Introduction to GCD recap

1. Blocks

- dispatch_async()

2. Queues

- Lightweight list of blocks

- Enqueue/dequeue is FIFO

3. dispatch_get_main_queue()

- Main thread/main runloop

4. dispatch_queue_create()

- Automatic helper thread

GCD Advantages

1. Efficiency - More CPU cycles available for your code

2. Better metaphors

- Blocks are easy to use

- Queues are inherently producer/consumer

3. Systemwide perspective

- Only the OS can balance unrelated subsystems

Compatibility

1. Existing threading and synchronization primitives are 100% compatible

2. GCD threads are wrapped POSIX threads

- Do not cancel, exit, kill, join, or detach GCD threads

3. GCD reuses threads

Thursday, April 07, 2011

WWDC2010 Session206 Introducing Blocks and Grand Central Dispatch (2)

Grand Central Dispatch

With GCD, you can make your app responsive. Threading is hard. Using GCD makes it simple and fun. You need not do explicit thread management. Cool!

Keeping your app responsive:

1. Do not block the main thread

2. Move work to another thread

3. Update UI back on main threaad

Code without GCD:

- (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url {

  // Controller UI callback on main thread

  DTweet *tw = [[DTweet alloc] initWithMsg:msg];

  [tweets addTweet:tw display:YES];

  tw.img = [imageCache getImgFromURL:url];//bottle neck

  [tweets updateTweet:tw display:YES];

  [tw release];

}

Code with GCD:

- (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url {

  // Controller UI callback on main thread

  DTweet *tw = [[DTweet alloc] initWithMsg:msg];

  [tweets addTweet:tw display:YES];

  dispatch_async(image_queue, ^{

    tw.img = [imageCache getImgFromURL:url];

    dispatch_async(main_queue, ^{

      [tweets updateTweet:tw display:YES];

    });

  });

  [tw release];

}

GCD Queues

1. Lightweight list of blocks

2. Enqueue/dequeue is FIFO

3. Enqueue with dispatch_async()

4. Dequeue by automatic thread or main thread

Main Queue

1. Executes blocks one at a time on main thread

2. Cooperates with the UIKit main run loop

3. dispatch_get_main_queue()

Wednesday, April 06, 2011

WWDC2010 Session206 Introducing Blocks and Grand Central Dispatch (1)

Blocks and Grand Central Dispatch are available on iOS4, and have been available on Snow Leopard.

Technology Stack

Technology Stack Block and Grand Central Dispatch  

Blocks

Blacks are available in C++ and Objective-C++.

Basic Blocks

We will use ^ for blocks because this character is unique and could not be used as operator in C++.

Block Literal Syntax

^ [Return type][Arguments] { Body }

Just looks like a function without function name starting with ^. If no return or no arguments, void could be skipped.

Block Syntax

Blocks as Data

We can define Block pointer. It looks like a function pointer: void (*callable)(void);

void (^callable)(void);

This is an ugly block pointer whose argument is also a block pointer.

char *(^worker)(char *a, BOOL(^done)(int));

We can use typedef to simplify it just like what we do for function pointers:

typedef BOOL (^doneBlk_t)(int);

char *(^workB)(char *a, doneBlk_t d);

WWDC2010 Session313 LLVM Technologies in Depth

This session covers: Clang in Xcode 4 (Code completion, Fix-it, Indexing and Edit-all-in-scope), Clients of LLVM(LLDB, Integrated assembler).

Using Clang Inside Xcode 4

Many contents have been covered in Session 312. Just more examples.

Benefits of LLDB Design

Higher fidelity expression parsing and evaluation

Support all language constructs

     Inline function, template instantiation   

     Debugger gets new language features

Less platform specific knowledge in the debugger

Can pull in other compiler features: Fix-it, code completion

Assembler

LLVM Compiler 2.0 have an integrated assembler. We need not generate .s file and parse big text files. It saves much time.

LLVM assembler

Benefits of LLVM Integrated Assembler:

10% f aster builds (for debug builds of many applications)

Better error messages for inline assembly

More useful assembly dumps

WWDC2010 Session313 LLVM Technologies in Depth

Author: Ted Kremenek - Manager, Compiler Frontend Team