I want to rotate my UISlider in iPhone landscape layouts and am using this code to to it.
self.customSlider.transform = CGAffineTransformMakeRotation(M_PI/2);
However, I want to use it in -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator so that it rotates appropriately based on size classes.
I have the following code where I put other rotation code
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
self.stillCamera.outputImageOrientation = UIInterfaceOrientationLandscapeRight;
}
if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
self.stillCamera.outputImageOrientation = UIInterfaceOrientationLandscapeLeft;
}
if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
self.stillCamera.outputImageOrientation = UIDeviceOrientationPortrait;
}
}
I want to make sure that the UISlider is in the proper orientation in each orientation with the left edge of the slider on top in landscape and the right on the bottom. I need to check the rotation to make sure that slider is in the right position regardless of how the user rotates from portrait to landscape left or right. How can I do this?
EDIT:
I am using GPUImage and the preview view uses an orientation left/right for that view, so I need to find an implementation that allows me to keep that while also allowing for general size class customization.
I figured it out
First - place the UISlider in a container view and pin that container view as needed.
Pin the UISlider to the container view in portrait - either centered horizontally/vertically with equal height/width to container.
In landscape, pin container view as needed with UISlider centered H/V with equal width to container.
In
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
self.stillCamera.outputImageOrientation = UIInterfaceOrientationLandscapeRight;
self.customSlider.transform = CGAffineTransformMakeRotation(M_PI/2);
}
if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
self.stillCamera.outputImageOrientation = UIInterfaceOrientationLandscapeLeft;
self.customSlider.transform = CGAffineTransformMakeRotation(M_PI/2);
}
if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
self.stillCamera.outputImageOrientation = UIDeviceOrientationPortrait;
self.customSlider.transform = CGAffineTransformIdentity;
}
}
Related
My Storybuilder is designed with a portrait layout. When I start the app with my iPad already turned to horizontal, it's able to correctly detect it's in a horizontal position. But when I start the app with my iPad in a portrait position, it thinks it's in horizontal. However, every time I rotate it, the code is able to detect the correct orientation properly.
- (void) viewDidLoad
{
[self updateForOrientation];
}
- (void)updateForOrientation
{
if (UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) // became portrait
{
NSLog(#"is portrait");
//code for changing layout to portrait position
}
else //became horiztontal
{
NSLog(#"is horizontal");
//code for changing layout to horizontal position
}
}
Output: is horizontal (this is the output whether it starts up as portrait or landscape)
The problem is that you're sending the devices orientation in terms of the UIDeviceOrientation enum to a function that's expecting a UIInterfaceOrientation value.
If you command click on UIInterfaceOrientationIsPortrait(), you can see that it is defined as follows.
#define UIInterfaceOrientationIsPortrait(orientation) ((orientation) == UIInterfaceOrientationPortrait || (orientation) == UIInterfaceOrientationPortraitUpsideDown)
And if you look at the enum declarations for the two orientation types (documentation links below), you can see that there is a misalignment in value due to the device orientation containing a value for "none". Anyway, changing your code to use UIInterfaceOrientation should sort this out. Example:
- (void)updateForOrientation
{
UIInterfaceOrientation currentOrientation = self.interfaceOrientation;
if (UIInterfaceOrientationIsPortrait(currentOrientation)) {
NSLog(#"is portrait");
}else{
NSLog(#"is horizontal");
}
}
https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIInterfaceOrientation
https://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/doc/c_ref/UIDeviceOrientation
I have a UITextView that in portrait sizes 500px width.
I add it by code as subview but i can't be able in viewdidapper to get the correct size if the app is opened in landscape mode.
After the device rotation it works right. If opened landscape no.
How can i calculate the correct dimension of this textview when opening landscape?
Thank you
Add a notifier in the viewWillAppear function
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
The orientation change notifies this function
- (void)orientationChanged:(NSNotification *)notification{
[self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}
and you calculate the size of your textView
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//calculate the portrait textView size
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//calculate the landscape textView size
}}
i hope this help
I have two views in my app; "Calculator" and the "Tape". I can click on the button inside calculator to get to the tape and vice versa. I set the rotation as per code below and it works fine most of the time.
However there are issues if I rotate either calculator view or tape view to landscape and then if I try to access other view, interface is all messed up like it doesn't recognize that device was already rotated. Any suggestions?
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self doLayoutForOrientation:toInterfaceOrientation];
}
- (void)doLayoutForOrientation:(UIInterfaceOrientation)orientation {
if (UIInterfaceOrientationIsPortrait(orientation))
{
//set the frames here
}
else
{
//set the frames here
}
}
write these codes in the view will appear of each view.
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//adjust the view for landscape
}
else if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//adjust the view for portrait.
}
Does anyone know fix(not to rotate) a certain UIView with allowing rotation of UIViewController?
I want to implement a interface like camera app UI. A view showing camera's view does not rotate but HUD(like buttons) does rotate.
You can apply transformation to the view you don't want to rotate.
Use following method to do it.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
view.transform = CGAffineTransformMakeRotation(-M_PI/2);
}
else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
view.transform = CGAffineTransformMakeRotation(M_PI/2);
}
return YES;
}
The first time I turn my iphone turns the button. The second time I turn the iphone fails.
- (void)configureViewsLandscapeMode
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (deviceOrientation == UIDeviceOrientationLandscapeLeft) {
[self.button setTransform:CGAffineTransformMakeRotation(M_PI_2)];
} else if (deviceOrientation == UIDeviceOrientationLandscapeRight) {
[self.button setTransform:CGAffineTransformMakeRotation(-M_PI_2)];
}
}
I read others similar answers:
UIView scale to 0 using CGAffineTransformMakeScale
CGAffineTransformInvert: singular matrix
I'm starting with IOS and I do not understand the problem. I would like to understand the problem more than having a solution, I would appreciate some guidance
When making a rotation animation, make sure you are not changing view frame. That can happen for example when autoresizing is used or when you are trying to change frame explicitely.
If you change frame and transform at the same time, especially inside an animation, iOS tries to generate an animation from the original position to the target position. That involves some matrix calculations and that can end in an error if multiple changes (e.g. frame) are involved.
Try this :
- (void)configureViewsLandscapeMode
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (deviceOrientation == UIDeviceOrientationLandscapeLeft) {
[self.button setTransform:CGAffineTransformRotate(CGAffineTransformIdentity, M_PI_2)];
} else if (deviceOrientation == UIDeviceOrientationLandscapeRight) {
[self.button setTransform:CGAffineTransformRotate(CGAffineTransformIdentity, -M_PI_2)];
}
}