0

我看到很多人问这个问题,我阅读了所有内容,但仍然不知道该怎么做。我有一个启用分页的 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;
}

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

4

1 回答 1

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;
}
于 2012-08-05T13:53:50.107 回答