动态UIScrollView包含来自NSMutableArray的分页和数据

时间:2012-08-05 12:35:28

标签: iphone objective-c ios xcode uiscrollview

我看到很多人都在询问它,我读了所有内容,但仍然无法知道如何去做。我有一个启用了分页的UIScrollView,我也有一个带字符串的NSMutableArray。

我想要做的是UIScrollView将使用字符串向UITextView显示,当我切换到下一页时,它将显示NSMutableArray上的下一个项目。

出于某种原因,我无法让它发挥作用。

- (void)setupPage
{
    pagingView.delegate = self;

    [self.pagingView setBackgroundColor:[UIColor blackColor]];
    [pagingView setCanCancelContentTouches:NO];

    pagingView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    pagingView.clipsToBounds = YES;
    pagingView.scrollEnabled = YES;
    pagingView.pagingEnabled = YES;
    pagingView.backgroundColor = [UIColor clearColor];
    [pagingView setShowsHorizontalScrollIndicator:NO];
    [pagingView setShowsVerticalScrollIndicator:NO];

    for (int i=0;i<[arrCat count];i++)
    {
        UITextView * txtJoke = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
        [pagingView addSubview:txtJoke];

        txtJoke.text = [arrCat objectAtIndex:i];
        txtJoke.textAlignment = UITextAlignmentRight;
    }
    [pagingView setContentSize:CGSizeMake(960, [pagingView bounds].size.height)];
}

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    if (pageControlIsChangingPage) {
        return;
    }

    /*
     *  We switch page at 50% across
     */
    CGFloat pageWidth = _scrollView.frame.size.width;
    int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    //pageControl.currentPage = page;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
    pageControlIsChangingPage = NO;
}

- (IBAction)changePage:(id)sender :(int)current
{
    /*
     *  Change the scroll view
     */
    CGRect frame = pagingView.frame;
    frame.origin.x = frame.size.width * 1;
    frame.origin.y = 0;

    [pagingView scrollRectToVisible:frame animated:YES];

    /*
     *  When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off
     */
    pageControlIsChangingPage = YES;
}

这段代码是我试图融入其中的其他应用程序的组合,但没有任何成功。 请帮忙。非常感谢。

1 个答案:

答案 0 :(得分:1)

首先,所有UITextView都具有相同的帧坐标,因此它们将相互叠加。假设您正在水平滚动,则需要将页面宽度偏移UITextView的x原点。

您使用的是UIPageControl吗?如果没有,您可以删除setupPage以外的方法。这些其他方法仅用于更改UIPageControl,并使用UIPageControl更改页面。

这是一个类似于你的基本滚动视图实现:

static NSUInteger kNumberOfPages = 3;
static NSUInteger kPageWidth = 320;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(kPageWidth * kNumberOfPages, self.scrollView.frame.size.height);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.delegate = self;

    self.pageOne.frame = CGRectMake(0, 0, kPageWidth, self.pageOne.frame.size.height);
    [self.scrollView addSubview:self.pageOne];

    self.pageTwo.frame = CGRectMake(kPageWidth, 0, kPageWidth, self.pageTwo.frame.size.height);
    [self.scrollView addSubview:self.pageTwo];

    self.pageThree.frame = CGRectMake(2*kPageWidth, 0, kPageWidth, self.pageThree.frame.size.height);
    [self.scrollView addSubview:self.pageThree];

    self.pageControl.numberOfPages = kNumberOfPages;
    self.pageControl.currentPage = 0;
}

# pragma mark - Scroll View Related Methods

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
    // which a scroll event generated from the user hitting the page control triggers updates from
    // the delegate method. We use a boolean to disable the delegate logic when the page control is used.
    if (pageControlUsed_)
    {
        // do nothing - the scroll was initiated from the page control, not the user dragging
        return;
    }

    // Switch the indicator when more than 50% of the previous/next page is visible
    int page = floor((self.scrollView.contentOffset.x - kPageWidth / 2) / kPageWidth) + 1;
    self.pageControl.currentPage = page;
}

// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    pageControlUsed_ = NO;
} 

// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    pageControlUsed_ = NO; 
}

- (IBAction)changePage:(id)sender
{ 
    int page = self.pageControl.currentPage;

// update the scroll view to the appropriate page
    CGRect frame = self.scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [self.scrollView scrollRectToVisible:frame animated:YES];

// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
    pageControlUsed_ = YES;
}