Skip to content

Instantly share code, notes, and snippets.

@meiwin
Created January 15, 2015 17:04
Show Gist options
  • Save meiwin/a1bd9dc8c9a97d4a4756 to your computer and use it in GitHub Desktop.
Save meiwin/a1bd9dc8c9a97d4a4756 to your computer and use it in GitHub Desktop.
@implementation AutoRotatingWindow
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willRotate:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)willRotate:(NSNotification *)n
{
UIInterfaceOrientation o = [n.userInfo[UIApplicationStatusBarOrientationUserInfoKey] intValue];
CGSize s = [UIScreen mainScreen].bounds.size;
UIInterfaceOrientation co = [UIApplication sharedApplication].statusBarOrientation;
CGFloat w = s.width;
CGFloat h = s.height;
if (co == UIInterfaceOrientationLandscapeLeft || co == UIInterfaceOrientationLandscapeRight)
{
w = s.height;
h = s.width;
}
switch (o) {
case UIInterfaceOrientationPortrait:
self.bounds = CGRectMake(0, 0, w, h);
self.transform = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationLandscapeLeft:
self.bounds = CGRectMake(0, 0, h, w);
self.transform = CGAffineTransformMakeRotation(M_PI * 270.f/180.f);
break;
case UIInterfaceOrientationLandscapeRight:
self.bounds = CGRectMake(0, 0, h, w);
self.transform = CGAffineTransformMakeRotation(M_PI * 90.f/180.f);
break;
case UIInterfaceOrientationPortraitUpsideDown:
self.bounds = CGRectMake(0, 0, w, h);
self.transform = CGAffineTransformMakeRotation(M_PI * 360.f/180.f);
break;
default:
break;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment