How to make OpenGL ES template with toolbar autorotate
I've been trying to figure out how to do this for several days now with only limited success. I've managed to manually handle NSNotifications that tell me when the view orientation changes, then I use CGAffineTransformations to move my toolbar to the correct orientation. This开发者_高级运维 kind of works, but isn't very clean. So my question is, how can I add a toolbar to the OpenGL-ES view and have it autorotate? I figure it will involve creating a new viewController, then adding the OpenGL view and toolbar to this view, but I don't have enough experience working with views and subviews to know the correct way to do this or even if this is the correct approach. I tried doing it and failed miserably.
OK, I finally figured it out. It wasn't very intuitive, but it works. This answer came from: http://www.idevgames.com/forums/thread-1773.html
1) add new file... Cocoa Touch Classes->UIViewController subclass, and name it GLViewController 2) in GLViewController.m, add #import "PaintingView.h" at the top, and in the loadView method, add:
CGRect rect = [[UIScreen mainScreen] applicationFrame];
self.view = [[PaintingView alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];
and further down, make a modification:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
3) in AppController.m, add #import "GLViewController.h" at the top, and in applicationDidFinishLaunching, add this:
GLViewController *viewController = [[GLViewController alloc] init];
UIToolbar *mainTools = [UIToolbar new];
mainTools.frame = CGRectMake(0, 0, 300, 50);
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:@"Help!" style:UIBarButtonItemStyleBordered target:self action:nil];
[mainTools setItems:[NSArray arrayWithObjects:newButton, nil]];
[[viewController view] addSubview:mainTools];
[window addSubview:[viewController view]];
You'll have to change your GL transforms and touch coordinates to suit, but this'll get you autorotating.
Hope this is helpful to someone else besides myself.
精彩评论