The app launches itself in landscape orientation.
In this orientation,
the status bar is hidden because the
statusBarHidden
property of the
UIApplication
object is
true
.
The big green
View
occupies the entire window.
It has two white
subviews,
the paddle and the ball.
Drag the paddle up and down.
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.
The
CADisplayLink
can’t be
initialized
until the view has been created,
and the view is created after the
init
method of the view controller has finished.
That’s why we can’t initialize the
CADisplayLink
in the
init
method of the view ontroller.
Instead,
we declare the
CADisplayLink
to be
lazy
,
which initializes it when it is first mentioned,
which happens in
viewDidLoad
.
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
setNeedsDisplay
.
AppDelegate
:
does the expensive animation only when the app is visible on the screen.
ViewController
:
added the
CADisplayLink
property,
configured by the method
viewDidLoad
.
Added methods
supportedInterfaceOrientations
and
deinit
.
View
:
added four properties,
the
init
that takes an
NSCoder
,
and methods
bounds
,
touchesMoved(_:withEvent:)
,
and
move
.
Info.plist
:
added the property Initial interface orientation: Landscape (left home button).
The
CADisplayLink
documentation said to
import QuartzCore
,
but
ViewController.swift
compiled even when I didn’t.
Open the
Supporting Files folder and
add an Initial interface orientation to
Info.plist
.
In Supporting Files in the Project navigator, open
Info.plist
.
Initial interface orientation: String Landscape (left home button)
See
Launching
your iPhone Application in Landscape.
To tell you the truth, I just control-click on
Info.plist
in the Project Navigator and select
Open As → Source Code.
init
method of class
View
,
decrease (or increase)
the distances
dx
and
dy
.
print
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).
57099.920137308 57099.93683947 57099.95354097 57099.970200435 etc.Open the Macintosh Terminal window and print the reciprocal of the interval.
bc -l scale=5 1 / (57099.93683947 - 57099.920137308) 59.87248 control-dDo the iPhone tech specs give the refresh rate? It was 68 times per second on my Motorola Android phone.
override func prefersStatusBarHidden() -> Bool { return false; }