When we added a subview to a bigger view in Touch or Hit, the subview remained permanently inside the bigger view. We will now swap out the subview and replace it with a different subview 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 property
index
in class
View
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 a little view.
But the little views have no
touchesBegan(_:withEvent:)
method, so the
touchesBegan(_: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:)
.
This view is completely covered by one of the little views.
Whenever we insert a
LittleView
(a subview)
into the big
View
(the superview),
we make sure that the
LittleView
has the same size as the
View
.
We do this
in the
init
and
touchesBegan(_:withEvent)
methods of class
View
.
But there’s one complication.
When
init
is called,
the size of the superview is the dummy size 600 × 600.
By the time the superview is displayed on the screen,
the size of the superview has been changed to the size of the screen.
When the size of the superview changes,
we want the size of the subview
(both the width and the height)
to change with it.
That’s the purpose of the
autoresizingMask
property of the
LittleView
s.
Our classes
LittleView0
and
LittleView1
are very similar.
But they don’t have to be.
They could be totally different subclasses of
UIView
.
AppDelegate
ViewController
View
LittleView0
:
red.LittleView1
:
blue.super.init
in the
init
method of class
View
.
backgroundColor = UIColor.yellowColor();Then remove the above statement. To change the background to yellow, we must change the background of the superview of the big
View
,
which is the
window.
Add the following statement to the
application(_:didFinishLaunchingWithOptions:)
method of the
application
delegate.
window!.backgroundColor = UIColor.yellowColor();
UIViewAnimationOptions.TransitionFlipFromLeft
,
the left edge of the old view moves to the right,
and the right edge of the old view moves away from the user and to the left.
Try one of the following in place of
UIViewAnimationOptions.TransitionFlipFromLeft
.
Also see
UIBarButtonSystemItem.PageCurl
.
let newIndex: Int = 1 - index; //toggle the indexto the following?
//toggle the index var newIndex: Int; if index == 0 { newIndex = 1; } else { newIndex = 0; }
let newIndex: Int = 1 - index; //toggle the indexto whichever one of the following you feel is clearest. The
%
is the
remainder
operator.
//Advance to the next index. let newIndex: Int = (index + 1) % littleViews.count;
//Advance to the next index. var newIndex: Int = index + 1; if newIndex == 3 { newIndex = 0; }
//Advance to the next index. var newIndex: Int; if index == 0 { newIndex = 1; } else if index == 1 { newIndex = 2; } else { newIndex = 0; }
init
and
touchesBegan(_:withEvent:)
View
.
print("subviews.count = \(subviews.count)");