旋转 UIScrollView 时出现问题

发布于 2024-08-29 01:06:56 字数 2973 浏览 4 评论 0原文

我在尝试让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汐鸠 2024-09-05 01:06:56

听起来好像缺少 -setNeedsDisplay: 消息。

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{
 [self setupPage];

 // try this:
 [scrollView setNeedsDisplay:YES]; 
}

Sounds like it's missing a -setNeedsDisplay: message.

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{
 [self setupPage];

 // try this:
 [scrollView setNeedsDisplay:YES]; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文