I am using UIScrollView to show lots of subviews. Below is the code for creating scroll view:
ScrollView = [[UIScrollView alloc]initWithFrame:CGRectZero];
ScrollView.delegate = self;
ScrollView.showsHorizontalScrollIndicator = NO;
ScrollView.showsVerticalScrollIndicator = NO;
ScrollView.clipsToBounds = YES;
ScrollView.scrollEnabled = YES;
ScrollView.pagingEnabled = YES;
CGFloat widthOfScroll= (maxTotalColumnsVisibleAtAnyPoint * widthOfSubView );
ScrollView.frame = CGRectMake(0, 0, widthOfScroll, heightOfSubView);
CGSize contentSizeHeaderView = self.ScrollView.frame.size;
CGFloat contentSizeWidth = self.totalSubViews * self.widthOfSubView;
contentSizeHeaderView.width = contentSizeWidth;
[ScrollView setContentSize:contentSizeHeaderView];
THe code for adding controls to ScrollView and event handler for scrollView are below::
-(void)loadControlsInScroll{
UIView *tempSubView = nil;
CGFloat tempOriginX = 0;
@autoreleasepool {
for (int i = 0; i < self.totalSubViews; i++) {
tempSubView = [dataSource viewForScroll:i];
tempSubView.layer.borderColor = borderColor.CGColor;
tempSubView.layer.borderWidth = kBorderWidth;
tempSubView.frame = CGRectMake(tempOriginX, 0, widthOfSubView, heightOfSubView);
tempOriginX = tempOriginX + widthOfSubView;
[self.ScrollView addSubview:tempSubView];
}
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
///Event handler
}
I have enabled paging in this scroll view.
Let us assume that at max scrollView can hold 8 subviews in its frame.
CASE 1:
16 subviews are added to the scrollview. Now two page are displayed. Scrolling to both pages are working correctly. Touching subviews in both pages does not cause any incorrect behavior.
CASE 2:
9 subviews are added to the scroll view. Now also it is tow paged. First pages contain 1st-8th subviews. Page two contain 2nd-9th subviews. Now touching subviews in Page 1 works normal. Touching in Page 2 calls the event handler scrollViewDidScroll of UIScrollView.
MORE INFORMATION:
- This does not happen if PagingEnabled is set to NO.
Why this scrollViewDidScroll event gets called even though no scrolling was manually done? The event handler scrollViewWillBeginDragging is also called at this time?
UPDATE 1:
The real problem is that if PagingEnabled is YES, then whether delegate of scrollview is set or not, in case 2, the scroll view automatically scrolls to the previous page.