iPhone - Prohobit UIView from rotating when the device is turned - iphone

I haven't been able to figure this out. Could someone please help me? I am trying to stop one UIView in my app from rotating when the device is turned.
I am working on a drawing app. Right now, when the device is turned, all UI elements turn with it. What I am trying to do is have all the buttons, menus, etc. turn, but have my canvas UIView be static and ignore the rotation of the phone.

If the canvas view is a subview of a view that rotates, then it will rotate as well. It's inescapable.
If you have one element of a view that you do not want to appear to rotate, you have use a rotation transform to programmatically rotate that one view back to the orientation and frame you want it have.

You could use the following to prevent the view itself from rotating:
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return false;
}
Then manually rotate the individual elements you want rotated.

OP here. Rotating the view using CGAffineTransformMakeRotate() worked like a charm. Thanks, TechZen!

Related

How to handle UIImagePickerController overlay rotation in landscape?

You cannot subclass UIImagePickerController, but surely there is clean(not saying obvious or easy) way to keep camera feed as background of UIViewController and just make UIImagePickerController overlay to rotate like it would respond to shouldAutorotateToInterfaceOrientation: ?
I just want to UIImagePicker stay in its beloved portrait orientation, but I want rotate my UI buttons that I put into camera overlay. What I have now, is changing each element's orientation with CGAffineTransformMakeRotation() so it always stays at the same place, but rotates around each center.
I downloaded Layar app and the somehow achieved it... camera feed stays and UI buttons rotates (like UIViewController's style).
edit: I have to use iOS 5.1 and Xcode 4.2
edit2: for now I have this int DIRECTION and depending on what is the current orientation of the device I assign from 0 to 3, so I can decide with what angle to rotate all UI buttons. I do this inside shouldAutorotateToInterfaceOrientation: which is returning only YES for portrait and upside portrait.
You have to put constraints on the buttons and picker view.
Use this link to study AutoLayout concept by Ray Wenderlich
Hope this will help but you have to go through it thoroughly.

How to anchor a view relative to the side of the screen iPhone

I am creating an iOS app that creates layouts directly in code. I am used to using xib, but that is not an option for this project. I cannot figure out how to position an element fixed to the bottom/side of the screen. The solution I'm looking for will position it to the bottom in portrait, but on rotation will fix it to the right hand side (very similar to how the bottom bar works in the camera app). It's properly at the bottom of the screen in portrait, the problem is on rotate
Also I'm building for iOS 5 so I need a solution that does not use Auto-Layout.
Here is the code I have so far that I do on rotate to landscape. The idea is that I rotate 90(which is working) then use some sort of mask to anchor it to the left hand side. Where am I going wrong here? Is it even possible to anchor an object like this??
myView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin;
CGAffineTransform rotate90 = CGAffineTransformMakeRotation(degreesToRadians(-90));
myView.transform = rotate90;
Do not deal with the bar, but with the views inside your bar.
Try this :
1) Prevent your app from autoRotate using :
//For iOS 5 and lower
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return NO;
}
//For iOS 6 and upper
- (BOOL)shouldAutorotate {
return NO;
}
2) Rotate the view inside your bar and not your bar itself
Hope it will help you.
You need to use the NSLayoutConstraints for doing this.
I don't know much about the constraints coding, so please refer NSLayoutConstraint for more reference.
WWDC 2012 Session Videos about NSLayoutConstraints

Qualcomm AR Camera overlay view

I am using NinevehGL framework and Qualcomm AR in order to intract with 3D object , but I faced with a problem which is I can not overlay any view , button , text on the camera !!! or better to say add any view to subview !!
for example [self.view addSubView:myImage];
but when camera starts , nothing adds !
I would be grateful if you tell me how to do so ..
You need to make sure that your views are positioned correctly (center, bounds, and transform). It's a bit tricky because the AR camera is always displayed in landscape right orientation if you're using the default orientation.
You can display it in portrait orientation, but then you need to modify the values while configuring video background. I suggest to use the default landscape right orientation.

iPhone rotation woes

I have been spending many frustrating hours trying to get rotations working on the iPhone version of Tunepal.
Firstly, I have a tab bar controller, with a navigation controller controlling each of the views.
I actually only want one of my views to be able to rotate and that is the TuneDisplay.
I have a subclassed the UITabBarController and overridden the shouldAutorotateToInterfaceOrientation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
if (self.selectedViewController != nil)
{
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
else
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
In each of the view controllers for each of the tabs I have overridden the method and returned YES for each orientation I want to support. All well and good and everything works as it should. If I try and do a rotation on a tab that doesn’t support the rotation, nothing happens.
The problem occurs if I move from a tab thats rotated to a tab that isnt supposed to support that rotation. The new tab is displayed rotated too! Screenshots for all this are included here:
http://tunepal.wordpress.com/2010/04/20/rotation-woes/
Is there any way I can make it rotate back to portrait on tapping the tab?
I have tried the unsupported setOrientation trick, but firstly it doesnt work correctly and secondly I received a warning from Apple for including it in my last build.
If (as I suspect) there is no way to limit this behavior:
How do I make the microphone image scale when I rotate the device?
How do I make the buttons and the progress bar expand to fit the witdh of the toolbar?
Also, one of the tabs that rotates ok has a table, with a search bar. The first time I rotate to the right or to the left, I get a black bar to the right of the search bar. If I subsequently rotate back and rotate again, the bar disappears! I have enabled the struts and springs things on the search bar in the interface builder and it looks like it should behave correctly.
Any ideas about how to fix this?
Ideas, feedback much appreciated
Bryan
This isn't a full answer. Rotation is seriously inconsistent. You have done the right things. Several aspects don't work in the simulator, so you need to confirm all your testing on a device. Table headers and search bars don't resize to full width in older OS versions, so stick with 3.1.3 or higher.
Commonest problems:
implement the shouldAutorotateToInterfaceOrientation: to return YES;
if you use navigation controllers the root view controller must support the orientation;
if you have a toolbar the view controller for all items must support it;
and same for a tab bar controller.
You may need to turn on orientation notifications to get more useful information out of the device:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
Remember to turn it off and remove yourself from the notifications when you are done; this is supposed to have a high overhead.
Set the view contentMode property for your image to resize; together with autoresizingMask, which you are setting in IB, you should be OK.
Remember also that you can use two different nibs for portrait and landscape modes. There is an example project that sort of does this ("WhichWayIsUp"); see the View Controller Programming Guide also ("Creating an Alternate Landscape Interface").
If the rotation methods are being called, then the UI should be rotated consistently. You will find that they aren't always called when they should be.
It isn't hard to call a rotational transform on your views to force a rotation. It shouldn't be needed, but sometimes that's the only way they will rotate.
view.transform = CGAffineTransformMakeRotation(M_PI * n);
If you get it figured out, let us know.
It sounds like you are handling the rotation correctly while the tab is displayed. However, as you know, there's no quick way to switch rotations. What you will have to do is rotate the view yourself using CGAffineTransform. See this question: Is there a documented way to set the iPhone orientation?
To scale the image, you should be able to click the arrows inside the UIImageView housing the image in Interface Builder. There's a little arrow in the upper right hand corner you can click to see how the view behaves when it's rotated to make sure it scales correctly. But you'd probably be better off not scaling the image and hadling the rotation as in the answer to the linked question.

iPhone SDK: disabling cllipping for UIScrollView

I'm working on an iPhone game, and trying to use a UIScrollView to display some icons, which I then want to enable the user to drag off the bar being scrolled, onto another view (basically, dragging them from their hand into play on the game board). The problem is that the UIScrollView clips everything outside it's own bounds.
Here is a picture of what I'm trying to do:
Functionally, it actually works, in that you can drag the icons up to the white board fine...but you can't see them as you are dragging...which is not acceptable.
Does anyone know if you can temporarily disable the clipping that a scroll view does, or is there some way to get around it? Hacky or not, I would really like to make it happen.
Does anyone have any other possible solutions for this? Or maybe any alternate approaches? I've considered if maybe a page view might work, but haven't tried it yet...and it's not at all as good of a solution as the scroll view.
Worst case I can just go back to not having the bar scrollable, but that really puts a damper on some of my game mechanics, and I'm really not too excited about that.
I think you're looking for the clipsToBounds property of UIView. I've used it successfully with UIScrollView:
scrollView.clipsToBounds = NO;
However, the dragging you want to do from the scroll view to the game view may require you to remove the icon view from the scroll view, place it in the superview at a position corresponding to its visible position within the scroll view (calculated using the scroll view's origin and content offset), and have that track the user's finger movements. On a release of the touch, either drop it on the game view at the proper position or return it to the scroll view.
I'm not 100% sure I understand the question but I think you should look into the z order of the scrollview and the whiteboard. It may be that the drag is just going behind the whiteboard.
Failing that, it would be useful to see all the bounds of your view heirarchy.
I also think a better solution allround might be to create a "sprite" to animate underneath the players finger - you could offset the drawpoint of the sprite from the touchlocation so that the player can see what they are dragging.