Until now, when we have added a little view to a big view, the little view has remained in the big one. We will now swap out the little view and replace it with another little view while the app is running.
The big view contains an
array
of two little views, although only one of the little views is visible
(i.e., currently
added
to the big view) at any given moment.
The instance variable
index
in class
BigView
is the index in the array of the curently visible little view.
It is initialized to 0 and flips back and forth between 0 and 1.
When the big view detects a touch,
it changes the currently visible subview
by calling
transitionFromView:toView:duration:options:completion:
and updates the
index
accordingly.
The finger actually touches down inside of a little view.
But the little views have no
touchesEnded:withEvent:
method, so the
touchesEnded:withEvent:
of the big view is called instead.
The view that is rotated is the
superview
of the first argument of
transitionFromView:toView:duration:options:completion:
.
Our classes
LittleView0
and
LittleView1
are very similar.
But they don’t have to be.
They could be totally different subclasses of
UIView
.
main.m
FlipAppDelegate
BigView
LittleView0
LittleView1
backgroundColor
of the
BigView
.
To change the background to yellow,
we must change the background of the superview of the
BigView
,
which is the
window.
Add the following statement to the
application:didFinishLaunchingWithOptions:
method of the
application
delegate.
self.window.backgroundColor = [UIColor yellowColor];
UIViewAnimationOptionTransitionCurlUp
or
UIViewAnimationOptionTransitionCrossDissolve
in place of
UIViewAnimationOptionTransitionFlipFromLeft
.
UIViewAnimationOptionTransitionFlipFromLeft
,
UIViewAnimationOptionTransitionFlipFromRight
,
UIViewAnimationOptionTransitionCurlUp
,
UIViewAnimationOptionTransitionCurlDown
.
NSUInteger newIndex = 1 - index; //toggle the indexto the following. The
%
is the remainder operator.
NSUInteger newIndex = (index + 1) % views.count; //cycle the index
touchesEnded:withEvent:
.NSLog(@"self.subviews.count == %u", self.subviews.count);