Iphone 动态UIScrollView,包含来自NSMutableArray的分页和数据
我看到很多人在问这个问题,我读了所有的东西,但仍然不知道怎么做。我有一个启用分页的UIScrollView,还有一个带有字符串的NSMutableArray 我想做的是UIScrollView将向UITextView显示字符串,当我切换到下一页时,它将显示NSMutableArray上的下一项 由于某种原因,我不能让它工作Iphone 动态UIScrollView,包含来自NSMutableArray的分页和数据,iphone,objective-c,ios,xcode,uiscrollview,Iphone,Objective C,Ios,Xcode,Uiscrollview,我看到很多人在问这个问题,我读了所有的东西,但仍然不知道怎么做。我有一个启用分页的UIScrollView,还有一个带有字符串的NSMutableArray 我想做的是UIScrollView将向UITextView显示字符串,当我切换到下一页时,它将显示NSMutableArray上的下一项 由于某种原因,我不能让它工作 - (void)setupPage { pagingView.delegate = self; [self.pagingView setBackground
- (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;
}
-(无效)设置页面
{
pagingView.delegate=self;
[self.pagingView setBackgroundColor:[UIColor blackColor]];
[分页视图设置CancancelContentTouches:否];
pagingView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
pagingView.clipsToBounds=是;
pagingView.scrollEnabled=是;
pagingView.PaginEnabled=是;
pagingView.backgroundColor=[UIColor clearColor];
[分页视图设置ShowShorizontalsCrollinIndicator:否];
[分页视图设置显示垂直滚动指示器:否];
对于(int i=0;i首先,您的所有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;
}
这使它变得非常简单,只需偏移UITextView的帧,它就应该可以工作。是否可以在不使用PageControl的情况下使用分页?是的,对吗?是的,在滚动视图中不需要页面控件来进行分页。此外,我注意到您没有明确设置滚动视图的帧,大概宽度是320?我不想定义像这样的页面,我有一个NSMutableArray,其中有10个对象,我想将每个项目中的文本加载到UIScrollView中的每个页面,我希望它是动态的。如果您有那么多,最好只添加您需要的视图:您当前正在查看的页面上的视图,之前页面上的视图,以及a页面上的视图之后。滚动时,使用-(void)scrollViewDidScroll:(UIScrollView*)发送器检查页面是否已更改,并在滚动视图中添加和删除视图。