Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

130

CHAPTER 5: Autorotation and Autosizing

Implementing the Swap

Single-click BIDViewController.m to open your view controller’s implementation file for editing. First, at the top of the file, add the following C macro:

#define degreesToRadians(x) (M_PI * (x) / 180.0)

This macro just allows us to convert between degrees and radians, which we’ll need to do in our code to handle swapping in rotated views. Scroll down a little, and add the following method after the last @synthesize call. It’s a little scary looking, but don’t worry; we’ll explain what’s going on after you’ve finished typing.

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {

if (interfaceOrientation == UIInterfaceOrientationPortrait) { self.view = self.portrait;

self.view.transform = CGAffineTransformIdentity; self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0)); self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);

}

else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) { self.view = self.landscape;

self.view.transform = CGAffineTransformIdentity; self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90)); self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);

}

else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {

self.view = self.landscape;

self.view.transform = CGAffineTransformIdentity; self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90)); self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);

}

}

The method willAnimateRotationToInterfaceOrientation:duration: is actually a method from our superclass that we’ve overridden. This method is called as the rotation begins but before the rotation actually happens. Actions that we take in this method will be animated as part of the rotation animation.

In this method, we look at the orientation that we’re rotating to and set the view property to either landscape or portrait, as appropriate for the new orientation, which makes sure the appropriate view is being shown. We then call CGAffineTransformMakeRotation, part of the Core Graphics framework, to create a rotation transformation.

A transformation is a mathematical description of changes to an object’s size, position, or angle. Ordinarily, iOS takes care of setting the transform value automatically when the

www.it-ebooks.info

CHAPTER 5: Autorotation and Autosizing

131

device is rotated. However, it handles this only for views that are in the view hierarchy, which means only the view already being shown is updated properly.

When we swap in our new view here, the view we’re swapping in hasn’t been adjusted by the system, so we need to make sure that we give it the correct transform for it to display correctly. That’s what willAnimateRotationToInterfaceOrientation:duration: is doing each time it sets the view’s transform property. Once the view has been rotated, we adjust its frame so that it fits snugly into the window at the current orientation.

Next, we need to implement our buttonTapped: method. Xcode has already created a stub implementation of this method for you. Add the following bold code to that existing method:

- (IBAction)buttonTapped:(id)sender {

NSString *message = nil;

if ([self.foos containsObject:sender]) message = @"Foo button pressed";

else

message = @"Bar button pressed";

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:message message:nil

delegate:nil

cancelButtonTitle:@"Ok"

otherButtonTitles:nil];

[alert show];

}

There’s nothing too surprising here. The outlet collections we created to point to the buttons are standard NSArray objects. To determine if sender is one of the Foo buttons, we simply check to see if foos contains it. If foos doesn’t, then we know it’s a Bar button.

Now, compile the app and give it a run.

Changing Outlet Collections

Our view-swapping app is obviously a rather simple example. In more complex user interfaces, you might need to make changes to user interface elements. In those cases, make sure that you make the same change to both the portrait and landscape versions.

Let’s see how that works. Let’s change the buttonTapped: method so that when a button is tapped, it disappears. We can’t just use sender for that because we need to also hide the corresponding button in the other orientation.

Replace your existing implementation of buttonTapped: with this one:

- (IBAction)buttonTapped:(id)sender {

if ([self.foos containsObject:sender]) { for (UIButton *oneFoo in foos) {

oneFoo.hidden = YES;

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]