旋转 UIScrollView 时出现问题
我在尝试让 pageControl 示例代码与旋转一起使用时遇到问题。我设法让它旋转,但在我开始滚动之前它在视觉上无法正确加载(然后它工作正常)。关于如何解决这个问题有什么想法吗?如果您想在 action 中查看该项目,这里有一个链接。
该代码基于苹果提供的 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
I am having issues trying to get the pageControl sample code to work with rotation. I managed to get it to rotate but it does not visually load correctly until I start to scroll (then it works fine). Any Idea on how I can fix this problem? Here is a link to the project if you want to see it in action.
This code is based off the PageControl example apple has provided.
here is the code:
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来好像缺少
-setNeedsDisplay:
消息。Sounds like it's missing a
-setNeedsDisplay:
message.