简体   繁体   English

UIScrollView分页有2个视图

[英]UIScrollView paging with 2 views

I tried to implement paging with 2 views from this tutorial: 我尝试使用本教程中的2个视图实现分页:

Multiple virtual pages in a UIScrollView with just 2 child views UIScrollView中的多个虚拟页面,只有2个子视图

I wanted to modify this by putting UIScrollView to pages and in every page updating it with new content: 我想通过将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)];
}

The idea is when scrolling just replace uiviewcontroller with new content. 这个想法是滚动时用新内容替换uiviewcontroller。 It calls: 它叫:

- (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;
        }
    }
} 

Well here I stuck. 好吧,我坚持了。 My text view is updating but new content of UIscrollView don't update in iPad simulator. 我的文本视图正在更新,但UIscrollView的新内容不会在iPad模拟器中更新。

Yea I mean in iphone it works while in iPad it don't. 是的,我的意思是在iphone中,它可以在iPad上运行,但它没有。 What I could improve if I want to achieve this functionality? 如果我想实现这个功能,我可以改进什么?

Maybe there are other way to do paging from iOS 5? 也许还有其他方法可以从iOS 5进行分页?

I would put source that a tried to make working. 我会把源代码试图让工作。

Download source 下载源代码

You can just use a nib file add a UIScrollView with fix width and height . 您可以使用nib文件添加具有固定widthheightUIScrollView Now make some pages as a UIView and add them to UIScrollView with same size and same origin . 现在将一些页面作为UIView ,并将它们添加到具有相同大小和相同origin UIScrollView In the main UIView : 在主要的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)];
}

And don't forget to check Paging Enabled for UIScrollView in the Attribute s in the nib file. 并且不要忘记在nib文件的Attribute s中检查UIScrollView Paging Enabled

I hope be useful for you! 我希望对你有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM