UIScrollView分页有2个视图

时间:2012-07-09 06:25:16

标签: objective-c ios ios5 uiscrollview

我尝试使用本教程中的2个视图实现分页:

Multiple virtual pages in a UIScrollView with just 2 child views

我想通过将UIScrollView放到页面并在每个页面中使用新内容更新它来修改它:

-(void)addContent:(NSString*)txt{

    int size  = 5;
    UITextView *txtView;
    int row = 0;

    //from left
    int xPlace = 0;
    //space between row's
    int rowSpace = 20;
    //from top
    int yPlace = 10;

   // int button_with = scrollView.frame.size.width;
    int button_height = 20;

    for (int i = 0; i < size; i++) {


        txtView = [[UITextView alloc] initWithFrame:CGRectMake(xPlace, row*rowSpace+yPlace, scrollView.frame.size.width, 2*button_height)];
        [txtView setFont:[UIFont fontWithName:@"ArialMT" size:14]];
        txtView.backgroundColor = [UIColor blueColor];

        [txtView setText:[NSString stringWithFormat:@"Numer    %@ - %i",txt,i]];

        // answerCheck.titleLabel.font = [UIFont boldSystemFontOfSize:fontSize];


        [scrollView addSubview:txtView];
        [txtView release];
        row++;
    }

    [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, txtView.frame.size.height*size)];
}

这个想法是滚动时用新内容替换uiviewcontroller。它叫:

- (void)setPageIndex:(NSInteger)newPageIndex
{
    pageIndex = newPageIndex;

    if (pageIndex >= 0 && pageIndex < [[DataSource sharedDataSource] numDataPages])
    {
        NSDictionary *pageData =
            [[DataSource sharedDataSource] dataForPage:pageIndex];
        label.text = @"Tekstas";//[pageData objectForKey:@"pageName"];
        textView.text = [pageData objectForKey:@"pageText"];
        [self addContent:[pageData objectForKey:@"pageText"]];
        CGRect absoluteRect = [self.view.window
            convertRect:textView.bounds
            fromView:textView];
        if (!self.view.window ||
            !CGRectIntersectsRect(
                CGRectInset(absoluteRect, TEXT_VIEW_PADDING, TEXT_VIEW_PADDING),
                [self.view.window bounds]))
        {
            textViewNeedsUpdate = YES;
        }
    }
} 

好吧,我卡住了。我的文本视图正在更新,但UIscrollView的新内容不会在iPad模拟器中更新。

是的,我的意思是在iphone中,它可以在iPad上运行,但它没有。如果我想实现这个功能,我可以改进什么?

也许还有其他方法可以从iOS 5进行分页?

我会把一个试图使用的源码。

Download source

1 个答案:

答案 0 :(得分:2)

您可以使用nib文件添加UIScrollView修复widthheight。现在将一些页面设为UIView并将其添加到UIScrollView,其大小相同且origin相同。在主UIView

- (void)viewDidLoad
{
    [super viewDidLoad];

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height * 2);
}

-(void)viewWillAppear:(BOOL)animated{
    CGFloat h = scrollView.frame.size.height;
    [pageView1 setFrame:CGRectMake(0, 0, pageView1.frame.size.width, pageView1.frame.size.height)];

    [pageView2 setFrame:CGRectMake(0, h, pageView2.frame.size.width, pageView2.frame.size.height)];
}

不要忘记在Paging Enabled文件的UIScrollView中查看Attribute nib

我希望对你有用!