我在尝试获取pageControl示例代码以使用旋转时遇到问题。我设法让它旋转但它没有正确加载,直到我开始滚动(然后它工作正常)。关于如何解决这个问题的任何想法?如果您想在action中看到它,可以使用以下链接指向该项目。
此代码基于Apple提供的PageControl示例。
这是代码:
#import "ScrollingViewController.h"
#import "MyViewController.h"
@interface ScrollingViewController (PrivateMethods)
- (void)loadScrollViewWithPage:(int)page;
@end
@implementation ScrollingViewController
@synthesize scrollView;
@synthesize viewControllers;
- (void)viewDidLoad
{
amount = 5;
[super viewDidLoad];
[self setupPage];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
[scrollView release];
}
- (void)dealloc
{
[super dealloc];
}
- (void)setupPage
{
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < amount; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * amount, 200);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
#pragma mark -
#pragma mark UIScrollViewDelegate stuff
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
if (pageControlIsChangingPage) {
return;
}
/*
* We switch page at 50% across
*/
CGFloat pageWidth = _scrollView.frame.size.width;
int dog = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
// pageControl.currentPage = page;
[self loadScrollViewWithPage:dog - 1];
[self loadScrollViewWithPage:dog];
[self loadScrollViewWithPage:dog + 1];
}
- (void)loadScrollViewWithPage:(int)page
{
if (page < 0) return;
if (page >= amount) return;
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self setupPage];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
@end
答案 0 :(得分:0)
听起来好像缺少-setNeedsDisplay:
消息。
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self setupPage];
// try this:
[scrollView setNeedsDisplay:YES];
}