In this app, unlike the previous one, it is entirely appropriate for the (big green) view to occupy the entire window. The status bar has a transparent background and is in front of the view.

The CADisplayLink in the ViewController calls the move: method of the View every time the hardware refreshes the display. It would be a waste of processing power to call move: more frequently than the hardware refreshes the display.

Since class View has no drawRect: method (other than the one that does nothing that is inherited from its base class UIView), it is not necessary to call [self setNeedsDisplay].

Source code in Pong.zip

  1. main.m
  2. Class PongAppDelegate
  3. Class ViewController
  4. Class View
  5. Pong-Info.plist

Create the project

ViewController.h must import QuartzCore/QuartzCore.h in order to mention CADisplayLink. Add an Initial Interface Orientation to Pong-Info.plist (see below).

Launch the in landcape orientation

In Supporting Files in the Project navigator, open Pong-Info.plist. Control-click the bottom line and select Add Row. Change the new row to
Initial interface orientation:     String     Landscape (left home button)
See Launching in Landscape Mode. To tell you the truth, I just control-click on Pong-Info.plist in the Project Navigator and select
Open As → Source Code.

	//In application:didFinishLaunchingWithOptions:
	CGRect bounds = [UIScreen mainScreen].bounds;

	NSLog(@"(%g, %g) %g × %g)",

	CGRect applicationFrame = [UIScreen mainScreen].applicationFrame;

	NSLog(@"(%g, %g) %g × %g)",
2013-11-12 21:11:39.439 Pong[13989:a0b] (0, 0) 320 × 568)
2013-11-12 21:11:39.440 Pong[13989:a0b] (20, 0) 300 × 568)

Things to try

  1. Make the ball move faster (or slower). In the initWithFrame: method of class View, decrease (or increase) the distances dx and dy.

  2. Uncomment the NSLog in the move: method of the View to find the interval in seconds between each refresh of the screen. On the simulator on my Mac, it was 60 times per second (except for the very first interval).
    2013-11-12 21:22:47.037 Pong[14031:a0b] 166640.820880753
    2013-11-12 21:22:47.051 Pong[14031:a0b] 166640.834562509
    2013-11-12 21:22:47.068 Pong[14031:a0b] 166640.851272424
    2013-11-12 21:22:47.084 Pong[14031:a0b] 166640.868001945
    2013-11-12 21:22:47.101 Pong[14031:a0b] 166640.884619043
    2013-11-12 21:22:47.118 Pong[14031:a0b] 166640.901141977
    Open the Macintosh Terminal window and print the reciprocal of the interval.
    bc -l
    1 / (166640.901141977 - 166640.884619043)
    Do the iPhone tech specs give the refresh rate? It was 68 times per second on my Motorola Android phone.

  3. Fix the bug in the logic. A collision is not detected when the ball hits a corner of the paddle.

  4. Play a sound each time the ball bounces off anything. Use this sound technology.