Making things Move on the iPhone

Post on 18-May-2015

3.703 views 2 download

Tags:

description

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

Transcript of Making things Move on the iPhone

Making Things Moveon the iPhone

Keith Peterswww.bit-101.com

Wednesday, March 4, 2009

www.bit-101.com/360iDev/

presentation.zip

Wednesday, March 4, 2009

CoreAnimation

Wednesday, March 4, 2009

Background

Wednesday, March 4, 2009

Background

2005

Wednesday, March 4, 2009

Background

2007

2005

Wednesday, March 4, 2009

Background

20082007

2005

Wednesday, March 4, 2009

Wednesday, March 4, 2009

Wednesday, March 4, 2009

Wednesday, March 4, 2009

What is Animation?

Wednesday, March 4, 2009

Wednesday, March 4, 2009

Wednesday, March 4, 2009

Coded Animation

Wednesday, March 4, 2009

Coded AnimationApply a

rule

Wednesday, March 4, 2009

Coded AnimationApply a

rule

Change something

Wednesday, March 4, 2009

Coded AnimationApply a

rule

Change something

Updatethe screen

Wednesday, March 4, 2009

Coded AnimationApply a

rule

Change something

Updatethe screen

Wednesday, March 4, 2009

Make somethingto move

Wednesday, March 4, 2009

Make somethingto move

Wednesday, March 4, 2009

Make somethingto move

(project files: Animation101)

Wednesday, March 4, 2009

// interfaceUIImageView *ball;

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

Wednesday, March 4, 2009

NSTimer

Wednesday, March 4, 2009

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

Wednesday, March 4, 2009

Moving UIViews

Wednesday, March 4, 2009

Moving UIViews

• view.center

Wednesday, March 4, 2009

Moving UIViews

• view.center

• view.transform

Wednesday, March 4, 2009

Moving UIViews

• view.center

• view.transform

• view.frame

Wednesday, March 4, 2009

// interfacefloat x;float y;

// viewDidLoadx = 50.0;y = 50.0;

Wednesday, March 4, 2009

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

Wednesday, March 4, 2009

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

Wednesday, March 4, 2009

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

Wednesday, March 4, 2009

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

Wednesday, March 4, 2009

Which is fastest?

Wednesday, March 4, 2009

Which is fastest?

• view.center

Wednesday, March 4, 2009

Which is fastest?

• view.center

• view.transform (~2-3x)

Wednesday, March 4, 2009

Which is fastest?

• view.center

• view.transform (~2-3x)

• view.frame (~2-3x)

Wednesday, March 4, 2009

Velocity

(project files: Velocity)

Wednesday, March 4, 2009

Velocity

+(project files: Velocity)

Wednesday, March 4, 2009

speedand

direction

Wednesday, March 4, 2009

speedand

direction

x velocity

y velocity

Wednesday, March 4, 2009

+ x velocity- x velocity

- y velocity

+ y velocity

Wednesday, March 4, 2009

Wednesday, March 4, 2009

// interface:float vx;float vy;

Wednesday, March 4, 2009

// interface:float vx;float vy;

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

Wednesday, March 4, 2009

// 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

speed

angle

(project files: AngularVelocity)

Wednesday, March 4, 2009

speed

x velocity = cos(angle) x speed

y velocity=

sin(angle)x

speed

angle

(project files: AngularVelocity)

Wednesday, March 4, 2009

Wednesday, March 4, 2009

// interface:float angle;float speed;

Wednesday, March 4, 2009

// interface:float angle;float speed;

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

Wednesday, March 4, 2009

// 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

Acceleration

(project files: Acceleration)

Wednesday, March 4, 2009

Acceleration

+(project files: Acceleration)

Wednesday, March 4, 2009

Wednesday, March 4, 2009

// interface:float ax;float ay;

Wednesday, March 4, 2009

// interface:float ax;float ay;

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

Wednesday, March 4, 2009

// 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

Bouncing

(project files: Bouncing)

Wednesday, March 4, 2009

Wednesday, March 4, 2009

x velocity

-x velocity

y velocity

y velocity

Wednesday, March 4, 2009

Wednesday, March 4, 2009

// interface:float bounce;

Wednesday, March 4, 2009

// interface:float bounce;

// viewDidLoad:bounce = -1.0;

Wednesday, March 4, 2009

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

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

Wednesday, March 4, 2009

Gravity

acceleration(+y)

Wednesday, March 4, 2009

Wednesday, March 4, 2009

// interface:float gravity;

Wednesday, March 4, 2009

// interface:float gravity;

// viewDidLoad:gravity = 0.5;

Wednesday, March 4, 2009

// interface:float gravity;

// viewDidLoad:gravity = 0.5;

// onTimer:vy += gravity;

Wednesday, March 4, 2009

Wednesday, March 4, 2009

acceleration.y

acceleration.x

Wednesday, March 4, 2009

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

Wednesday, March 4, 2009

Wednesday, March 4, 2009

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

Wednesday, March 4, 2009

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

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

Wednesday, March 4, 2009

// 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

Do we still have time?

Wednesday, March 4, 2009

Dragging and Throwing

Wednesday, March 4, 2009

Wednesday, March 4, 2009

// interfaceBOOL dragging;

Wednesday, March 4, 2009

// interfaceBOOL dragging;

// viewDidLoaddragging = NO;

Wednesday, March 4, 2009

// interfaceBOOL dragging;

// viewDidLoaddragging = NO;

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

Wednesday, March 4, 2009

- (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

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

Wednesday, March 4, 2009

- (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

Thank you

• Keith Peters

• kp@bit-101.com

• www.bit-101.com

• www.wickedpissahgames.com

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

Wednesday, March 4, 2009