I have a scrollview that automatically generates a series of subviews containing imageviews. The concept is that it's pretty much a custom PDF style reader, where each page is loaded in to an imageview as an image. There are three "layers" - the outer UIScrollView, the automatically gener开发者_运维知识库ated subViews and the imageview inside the subview.
My dilemma is that as these subviews are generated they don't really have a hard reference, so in my didRotateFromInterfaceOrientation there's no way to say resize THIS particular view.
I have created a function called setUpView which initialises all the views and images as so:
- (void)setUpView {
//INITIALISE PAGING SCROLL VIEW
CGRect pagingScrollViewFrame = [[UIScreen mainScreen] bounds];
pagingScrollViewFrame.origin.x -= 10;
pagingScrollViewFrame.size.width += 20;
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
//CONFIGURE PAGING SCROLL VIEW
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width*7, pagingScrollViewFrame.size.height);
//ACTIVATE PAGING SCROLL VIEW
self.view = pagingScrollView;
//ADD PAGES TO SCROLL VIEW
for (int i = 0; i < 7; i++){
ImageScrollView *page = [[[ImageScrollView alloc] init] autorelease];
[self configurePage:page forIndex:i];
[pagingScrollView addSubview:page];
}
}
In my loadView method I just call this function.
Is there any way to detect the current orientation of the device so that may be fed into the above function somehow? At the moment, on rotate, the outerview rotates fine, but the inner sub-views and the imageviews do not.
You can make the objects without reference observer for UIDeviceOrientationDidChangeNotification notification, which will be posted on every orientation change.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
Then, in your orientationChanged: method you can check current orientation and layout your views accordingly.
However, you need to tell the device to generate such notifications by calling
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
Be sure to remove the objects that are observing the orientation change on dealloc!
You can get device orientation by looking at:
[UIDevice currentDevice].orientation
or you could use self.interfaceOrientation
for viewController.
精彩评论