1 - 我创建了一个 uiviewcontroller 并将滚动视图放入其中。我正在尝试在页面之间导航,并且还在底部使用页面控制器。2 - 当我向滚动视图添加标签时,一切正常。3 - 但是当我使用以下代码将现有的 uiview 添加到我的滚动视图中时,滚动视图不会滚动到下一页;
- (void)viewDidLoad
{
scroller.contentSize = CGSizeMake(4608, 796);
contentView.frame = CGRectMake(2, 2, 768, 760); //this is my existing uiview which has some components (text box,image view etc.) in it.
// I am creating labels dynamically at specific positions
UILabel* label = [[UILabel alloc] init];
label.text = [NSString stringWithFormat:@"%d. pagem",0];
label.backgroundColor = [UIColor redColor];
label.frame = CGRectMake(1, 0, 768, 780);
// I create another uilabel here
UILabel* label2 = [[UILabel alloc] init];
label2.text = [NSString stringWithFormat:@"%d. pagem",1];
label2.backgroundColor = [UIColor greenColor];
label2.frame = CGRectMake(768 +1, 0, 768, 780);
[scroller addSubview:contentView]; // when i add uiview to the scoller, scroller not scoll to the next page
[scroller addSubview:label]; // scroller works fine when i remove the code above (adding uiview) and add this label.
[scroller addSubview:label2];
[self.scroller setDelegate:self];
pageControl.numberOfPages = 3;
pageControl.currentPage = 0;
[self changeThePage:nil];
[super viewDidLoad];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if (changeUsed) return;
float pageWidth = scroller.frame.size.width;
int page = floor((scroller.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
if (scroller.contentOffset.x >200)
{
page++;
}
if (page >-1 && page<6)
{
pageControl.currentPage = page;
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
changeUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
changeUsed = NO;
}
- (IBAction)changeThePage:(id)sender
{
int pagem = pageControl.currentPage;
NSLog(@"the page is : %d", pagem);
CGRect frame = scroller.frame;
frame.origin.x = frame.size.width * pagem;
frame.origin.y = 0;
[scroller scrollRectToVisible:frame animated:YES];
changeUsed = YES;
}
4 - 一旦我从 uiscrollview 及其声明中删除我现有的 uiview,一切都会再次正常运行。
不知道为什么uiview会阻止uiscrollview的滚动?