making things move on the iphone

88
Making Things Move on the iPhone Keith Peters www.bit-101.com Wednesday, March 4, 2009

Upload: john-wilker

Post on 18-May-2015

3.704 views

Category:

Technology


2 download

DESCRIPTION

Keith Peters - "Making things Move on the iPhone", 360|iDev San Jose '09

TRANSCRIPT

Page 1: Making things Move on the iPhone

Making Things Moveon the iPhone

Keith Peterswww.bit-101.com

Wednesday, March 4, 2009

Page 2: Making things Move on the iPhone

www.bit-101.com/360iDev/

presentation.zip

Wednesday, March 4, 2009

Page 3: Making things Move on the iPhone

CoreAnimation

Wednesday, March 4, 2009

Page 4: Making things Move on the iPhone

Background

Wednesday, March 4, 2009

Page 5: Making things Move on the iPhone

Background

2005

Wednesday, March 4, 2009

Page 6: Making things Move on the iPhone

Background

2007

2005

Wednesday, March 4, 2009

Page 7: Making things Move on the iPhone

Background

20082007

2005

Wednesday, March 4, 2009

Page 8: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 9: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 10: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 11: Making things Move on the iPhone

What is Animation?

Wednesday, March 4, 2009

Page 12: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 13: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 14: Making things Move on the iPhone

Coded Animation

Wednesday, March 4, 2009

Page 15: Making things Move on the iPhone

Coded AnimationApply a

rule

Wednesday, March 4, 2009

Page 16: Making things Move on the iPhone

Coded AnimationApply a

rule

Change something

Wednesday, March 4, 2009

Page 17: Making things Move on the iPhone

Coded AnimationApply a

rule

Change something

Updatethe screen

Wednesday, March 4, 2009

Page 18: Making things Move on the iPhone

Coded AnimationApply a

rule

Change something

Updatethe screen

Wednesday, March 4, 2009

Page 19: Making things Move on the iPhone

Make somethingto move

Wednesday, March 4, 2009

Page 20: Making things Move on the iPhone

Make somethingto move

Wednesday, March 4, 2009

Page 21: Making things Move on the iPhone

Make somethingto move

(project files: Animation101)

Wednesday, March 4, 2009

Page 22: Making things Move on the iPhone

// interfaceUIImageView *ball;

// viewDidLoadball = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"ball.png"]];[self.view addSubview:ball];

Wednesday, March 4, 2009

Page 23: Making things Move on the iPhone

NSTimer

Wednesday, March 4, 2009

Page 24: Making things Move on the iPhone

NSTimer[NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

Wednesday, March 4, 2009

Page 25: Making things Move on the iPhone

Moving UIViews

Wednesday, March 4, 2009

Page 26: Making things Move on the iPhone

Moving UIViews

• view.center

Wednesday, March 4, 2009

Page 27: Making things Move on the iPhone

Moving UIViews

• view.center

• view.transform

Wednesday, March 4, 2009

Page 28: Making things Move on the iPhone

Moving UIViews

• view.center

• view.transform

• view.frame

Wednesday, March 4, 2009

Page 29: Making things Move on the iPhone

// interfacefloat x;float y;

// viewDidLoadx = 50.0;y = 50.0;

Wednesday, March 4, 2009

Page 30: Making things Move on the iPhone

ball.center = CGPointMake(x, y);x += 2.0;y += 3.0;

Wednesday, March 4, 2009

Page 31: Making things Move on the iPhone

ball.frame = CGRectMake(x, y, width, height);x += 2.0;y += 3.0;

Wednesday, March 4, 2009

Page 32: Making things Move on the iPhone

ball.transform = CGAffineTransformMakeTranslation(x, y);x += 2.0;y += 3.0;

Wednesday, March 4, 2009

Page 33: Making things Move on the iPhone

ball.transform = CGAffineTransformTranslate( ball.transform, 2.0, 3.0);

Wednesday, March 4, 2009

Page 34: Making things Move on the iPhone

Which is fastest?

Wednesday, March 4, 2009

Page 35: Making things Move on the iPhone

Which is fastest?

• view.center

Wednesday, March 4, 2009

Page 36: Making things Move on the iPhone

Which is fastest?

• view.center

• view.transform (~2-3x)

Wednesday, March 4, 2009

Page 37: Making things Move on the iPhone

Which is fastest?

• view.center

• view.transform (~2-3x)

• view.frame (~2-3x)

Wednesday, March 4, 2009

Page 38: Making things Move on the iPhone

Velocity

(project files: Velocity)

Wednesday, March 4, 2009

Page 39: Making things Move on the iPhone

Velocity

+(project files: Velocity)

Wednesday, March 4, 2009

Page 40: Making things Move on the iPhone

speedand

direction

Wednesday, March 4, 2009

Page 41: Making things Move on the iPhone

speedand

direction

x velocity

y velocity

Wednesday, March 4, 2009

Page 42: Making things Move on the iPhone

+ x velocity- x velocity

- y velocity

+ y velocity

Wednesday, March 4, 2009

Page 43: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 44: Making things Move on the iPhone

// interface:float vx;float vy;

Wednesday, March 4, 2009

Page 45: Making things Move on the iPhone

// interface:float vx;float vy;

// viewDidLoad:vx = 2.0;vy = 3.0;

Wednesday, March 4, 2009

Page 46: Making things Move on the iPhone

// interface:float vx;float vy;

// viewDidLoad:vx = 2.0;vy = 3.0;

// onTimer:ball.center = CGPointMake(x, y);x += vx;y += vy;

Wednesday, March 4, 2009

Page 47: Making things Move on the iPhone

speed

angle

(project files: AngularVelocity)

Wednesday, March 4, 2009

Page 48: Making things Move on the iPhone

speed

x velocity = cos(angle) x speed

y velocity=

sin(angle)x

speed

angle

(project files: AngularVelocity)

Wednesday, March 4, 2009

Page 49: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 50: Making things Move on the iPhone

// interface:float angle;float speed;

Wednesday, March 4, 2009

Page 51: Making things Move on the iPhone

// interface:float angle;float speed;

// viewDidLoad:angle = 45.0;speed = 4.0;

Wednesday, March 4, 2009

Page 52: Making things Move on the iPhone

// interface:float angle;float speed;

// viewDidLoad:angle = 45.0;speed = 4.0;

// onTimer:ball.center = CGPointMake(x, y);x += cos(angle * M_PI / 180.0) * speed;y += sin(angle * M_PI / 180.0) * speed;

Wednesday, March 4, 2009

Page 53: Making things Move on the iPhone

Acceleration

(project files: Acceleration)

Wednesday, March 4, 2009

Page 54: Making things Move on the iPhone

Acceleration

+(project files: Acceleration)

Wednesday, March 4, 2009

Page 55: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 56: Making things Move on the iPhone

// interface:float ax;float ay;

Wednesday, March 4, 2009

Page 57: Making things Move on the iPhone

// interface:float ax;float ay;

// viewDidLoad:ax = 0.07;ay = 0.1;

Wednesday, March 4, 2009

Page 58: Making things Move on the iPhone

// interface:float ax;float ay;

// viewDidLoad:ax = 0.07;ay = 0.1;

// onTimer:ball.center = CGPointMake(x, y);x += vx;y += vy;vx += ax;vy += ay;

Wednesday, March 4, 2009

Page 59: Making things Move on the iPhone

Bouncing

(project files: Bouncing)

Wednesday, March 4, 2009

Page 60: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 61: Making things Move on the iPhone

x velocity

-x velocity

y velocity

y velocity

Wednesday, March 4, 2009

Page 62: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 63: Making things Move on the iPhone

// interface:float bounce;

Wednesday, March 4, 2009

Page 64: Making things Move on the iPhone

// interface:float bounce;

// viewDidLoad:bounce = -1.0;

Wednesday, March 4, 2009

Page 65: Making things Move on the iPhone

ball.center = CGPointMake(x, y); x += vx;y += vy;if(x > 300) // 320 - radius (20){ x = 300; vx *= bounce;}else if(x < 20) // 0 + radius{ x = 20; vx *= bounce;}

Wednesday, March 4, 2009

Page 66: Making things Move on the iPhone

if(y > 440) // 460 - radius{ y = 440; vy *= bounce;}else if(y < 20) // 0 + radius{ y = 20; vy *= bounce;}

Wednesday, March 4, 2009

Page 67: Making things Move on the iPhone

Gravity

acceleration(+y)

Wednesday, March 4, 2009

Page 68: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 69: Making things Move on the iPhone

// interface:float gravity;

Wednesday, March 4, 2009

Page 70: Making things Move on the iPhone

// interface:float gravity;

// viewDidLoad:gravity = 0.5;

Wednesday, March 4, 2009

Page 71: Making things Move on the iPhone

// interface:float gravity;

// viewDidLoad:gravity = 0.5;

// onTimer:vy += gravity;

Wednesday, March 4, 2009

Page 72: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 73: Making things Move on the iPhone

acceleration.y

acceleration.x

Wednesday, March 4, 2009

Page 74: Making things Move on the iPhone

@interface GravityViewController : UIViewController <UIAccelerometerDelegate>{ UIImageView *ball; float x; float y; float vx; float vy; float bounce; CGPoint gravity;}

Wednesday, March 4, 2009

Page 75: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 76: Making things Move on the iPhone

// viewDidLoadgravity = CGPointZero;[[UIAccelerometer sharedAccelerometer] setDelegate:self];

Wednesday, March 4, 2009

Page 77: Making things Move on the iPhone

// viewDidLoadgravity = CGPointZero;[[UIAccelerometer sharedAccelerometer] setDelegate:self];

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{ gravity = CGPointMake(acceleration.x, -acceleration.y);}

Wednesday, March 4, 2009

Page 78: Making things Move on the iPhone

// viewDidLoadgravity = CGPointZero;[[UIAccelerometer sharedAccelerometer] setDelegate:self];

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{ gravity = CGPointMake(acceleration.x, -acceleration.y);}

// onTimervx += gravity.x;vy += gravity.y;

Wednesday, March 4, 2009

Page 79: Making things Move on the iPhone

Do we still have time?

Wednesday, March 4, 2009

Page 80: Making things Move on the iPhone

Dragging and Throwing

Wednesday, March 4, 2009

Page 81: Making things Move on the iPhone

Wednesday, March 4, 2009

Page 82: Making things Move on the iPhone

// interfaceBOOL dragging;

Wednesday, March 4, 2009

Page 83: Making things Move on the iPhone

// interfaceBOOL dragging;

// viewDidLoaddragging = NO;

Wednesday, March 4, 2009

Page 84: Making things Move on the iPhone

// interfaceBOOL dragging;

// viewDidLoaddragging = NO;

- (void)onTimer{ if(!dragging) { ... }}

Wednesday, March 4, 2009

Page 85: Making things Move on the iPhone

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self.view]; float dx = point.x - x; float dy = point.y - y; float dist = sqrt(dx * dx + dy * dy); if(dist < 20) { dragging = YES; x = point.x; y = point.y; vx = 0; vy = 0; }}

Wednesday, March 4, 2009

Page 86: Making things Move on the iPhone

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ dragging = NO;}

Wednesday, March 4, 2009

Page 87: Making things Move on the iPhone

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ if(dragging) { UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self.view]; vx = point.x - x; vy = point.y - y; x = point.x; y = point.y; ball.center = point; }}

Wednesday, March 4, 2009

Page 88: Making things Move on the iPhone

Thank you

• Keith Peters

[email protected]

• www.bit-101.com

• www.wickedpissahgames.com

• www.bit-101.com/360iDev/presentation.zip

Wednesday, March 4, 2009