Skip to content

Instantly share code, notes, and snippets.

@joshaber
Created September 11, 2010 07:52
Show Gist options
  • Save joshaber/574966 to your computer and use it in GitHub Desktop.
Save joshaber/574966 to your computer and use it in GitHub Desktop.
- (void)floatIntoWindow {
if(self.superview == self.window) return;
CGRect newFrame = [self.superview convertRect:self.frame toView:nil];
[self.window addSubview:self];
self.transform = [self transformForCurrentInterfaceOrientation];
self.frame = newFrame;
}
- (void)unfloatIntoView:(UIView *)newSuperview {
if(self.superview == newSuperview) return;
CGRect newFrame = [newSuperview convertRect:self.frame fromView:nil];
[newSuperview addSubview:self];
self.transform = newSuperview.transform;
self.frame = newFrame;
}
- (CGAffineTransform)transformForInterfaceOrientation:(UIInterfaceOrientation)orientation {
if(orientation == UIInterfaceOrientationPortrait) {
return CGAffineTransformIdentity;
} else if(orientation == UIInterfaceOrientationLandscapeLeft) {
return CGAffineTransformMakeRotation(270.0f * (CGFloat) M_PI / 180.0f);
} else if(orientation == UIInterfaceOrientationLandscapeRight) {
return CGAffineTransformMakeRotation(90.0f * (CGFloat) M_PI / 180.0f);
} else if(orientation == UIInterfaceOrientationPortraitUpsideDown) {
return CGAffineTransformMakeRotation(180.0f * (CGFloat) M_PI / 180.0f);
}
return CGAffineTransformIdentity;
}
- (CGAffineTransform)transformForCurrentInterfaceOrientation {
return [self transformForInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment