Simulating a orientation change on a UIViewController - animation

I have a root UIViewController that handles multiple sub UIViewControllers. The root view is a custom tab bar that does not rotate. I want the sub controllers to rotate with orientation changes.
From some testing, I've learned that the root controller handles all orientation changes for its sub controllers. So I need to implement a custom animation that would mimic a standard device orientation change, that I can apply to the visible view and, use to transform all non-visible views.
The visible sub controller's view is added as a sub view to the root's "contentView". There is only one sub controller view in the "contentView" at a time. The rest are in an array.
How would I do something like this? That supports iOS 5 and 6. I know I need to preform a CGTransformation to rotate the view, and resize the frame to match. Mainly, I'm not sure what steps I need to take to ensure proper functioning of the sub controllers.

I figured out that after applying CGAffineTransformations to a UIView, you can no longer reliably edit the view's frame. you need to use the bounds instead. keeping the origin of the bounds the same, and changing the size, you can change the view's size after a Pi/2 rotation. Good for faking a UIDevice rotation change, or responding to one in a root view that does'n allow for orientation changes.

Related

Rotating UI objects in locked position View in Swift iOS 8

I have a question. I'm writing an app where in one of its views I want to achieve the next:
the view itself doesnt rotate meaning all the controls etc are pinned to their places BUT the objects (buttons for example) DO rotate on their place place.
The problem is that Autorotate is either rotates everything, including position change of the buttons, or locks everything including keeping button orientation.
From what I understood I can lock the view and do a manual transform every time orientation changes but it seems to me very long and ugly, when Xcode has now all this Autolayout and class sizes etc ...
Is there a more elegant way to do it ?

isOpaque not stopping passing to parents drawRect

I got a problem with Cocoa and its View redraw hierarchy.
I'm currently testing displaying (audio) levels in a meter style control and I'm using the MeteringView class from MatrixMixerTest example project from apple. This class is drawing the meter and only drawing the difference what got changed which looks like a very efficient class.
My project is splitted into 2 splitviews, in some are NSCollectionViews (Scrollview, Clipview) and in others are only static views. If I add the meter to those "static" views they work fine when these views call setNeedsDisplay:YES. If a meter is added to the view of a CollectionView Item it gets rendered, but loosing its drawn "old level" parts and its corners/background. I think this happens because the CollectionView item gets also called to be redrawn (which has a background image) and everything is gone. It is drawing some parts whats currently changing (the drawing works).
Is there a way to prevent the Item itself to be redrawn? Or, I dont know why it is not happening in those static views, because those views also have background images but do not draw over the meter.
Are there some tricks or whats different in a CollectionView than in a "normal" view?
EDIT: After reading about isOpaque (MeteringView isOpaque = YES) means it should not call the parent views drawRect if set to yes. Well that works for the static views, those MeteringViews do not call parents drawRect, but those in a CollectionView do however. I dont know why.
EDIT 2: I gave this topic another title, because isOpaque=YES in MeteringView is not stopping calling the parents drawRect in a CollectionView, in a normal view it is working. Are there some things to know about? I have to stop redrawing the CollectionView Item because thats the problem.
Thanks in advance guys
Benjamin
isOpaque is just hint to the system. It does not prevent other views from drawing their contents, it only means that it can sometimes skip making other views update their contents.
If your view is opaque, it should draw itself as opaque and completely fill its bounds.

iOS - passing information between subviews within a single view controller

In my drawing app I have a switch called "Snap to grid" -- When its state is changed I need drawing in my canvas view to honor that change. At present, the switch is a subview of a UIView so that the text label and the switch can travel together during a device orientation change.
I'm trying to decide what is the best way of communicating the state change. My thought train has been comparing this to using jQuery selector, like a $("#switch") -- in essence, when the view controller has instantiated the subviews (and subviews of those subviews), how do you get a pointer to the specific subview you're looking for?
I should add that my views are created in the Interface Builder portion of Xcode, so they are loaded from a .nib file rather than me creating them (which obviously would give me a chance to hold on to a pointer to the subviews I need).

How to "stick" a UIScrollView subview to top/bottom when scrolling?

You see this in iPhone apps like Gilt. The user scrolls a view, and a subview apparently "sticks" to one edges as the rest of the scrollView slides underneath. That is, there is a text box (or whatever) in the scrollView, that as the scrollView hits the top of the view, then "sticks" there as the rest of the view continues to slide.
So, there are several issues. First, one can determine via "scrollViewDidScroll:" (during normal scrolling) when the view of interest is passing (or re-appearing). There is a fair amount of granularity here - the differences between delegate calls can be a hundred of points or more. That said, when you see the view approach the top of the scrollView, you turn on a second copy of the view statically displayed under the scrollView top. I have not coded this, but it seems like it will lack a real "stick" look - the view will first disappear then reappear.
Second, if one does a setContentOffset:animated, one does not get the delegate messages (Gilt does not do this). So, how do you get the callbacks in this case? Do you use KVO on "scroll.layer.presentationLayer.bounds" ?
Well, I found one way to do this. When the user scrolls by flicking and dragging, the UIScrollView gives its delegate a "scrollViewDidScroll:" message. You can look then to see if the scroller has moved the content to where you need to take some action.
When "sticking" the view, remove it from the scrollView, and then add it to the scrollView's superview (with an origin of 0,0). When unsticking, do the converse.
If you use the UIScrollView setContentOffset:animated:, it gets trickier. What I did was to subclass UIScrollView, use a flag to specify it was setContentOffset moving the offset, then start a fast running timer to monitor contentOffset.
I put the method that handles the math and sticking/unsticking the child view into this subclass. It looks pretty good.
Gilt uses a table view to accomplish this. Specifically, in the table view's delegate, these two methods:
– tableView:viewForHeaderInSection:
and – tableView:heightForHeaderInSection:

iPad - Handling orientation change via reloaded loadView

I have a custom view which needs to be relaid on orientation change. My UIView code can correctly redraw for the any orientation as it express itself in screen frame size.
But how do I get the loadView method to run on orientation change for the right view to get drawn ?
I put [self.view setNeedsDisplay] in the following rotation handling methods with no luck.
willRotateToInterfaceOrientation:duration
didRotateFromInterfaceOrientation:
Any suggestions would be most welcome.
loadView actually causes the view to be completely re-created from scratch. It should be sufficient to call setNeedsDisplay in didRotateFromInterfaceOrientation on the view that needs to redraw.
If you have to change the layout of your views upon rotation, do so in willAnimateRotationToInterfaceOrientation (so the changes get animated), and/or set the autoresizingMask property to the combination of UIViewAutoresizingMask values that suits your needs.

Resources