Paging uiscrollview automatically scrolls to the first page
I've been working on a paging scrollview with several images and a tableview as a table of content. Using the table I can jump to specific pages. I've got everything working except the jump to page method which when I touch the screen brings the scroll view to the first page UNLESS I scroll to the next page. It's been driving me nuts for the last couple of days :s
here's my code :
-(void)skipToPage {
NSInteger temp = [selectedPage integerValue];
[pagingScrollView setContentOffset:
[self offsetForPageAtIndex:temp] animated:NO];
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
}
- (CGPoint)offsetForPageAtIndex:(NSUInteger)index {
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
CGPoint offset;
offset.x = (pagingScrollViewFrame.size.width * index);
offset.y = 0;
return offset;
}
Thanks in advance fellas!
EDIT: scrolviewDidScroll calls the Tile page method which prepares each image to be displayed
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self tilePages];
}
It's a method from the photoScroller sample from Apple
- (void)tilePages
{
// Calculate which pages are visible
CGRect visibleBounds = pagingScrollView.bounds;
int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
int lastNeededPageIndex = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
lastNeededPageIndex = MIN(lastNeededPageIndex, [self imageCount] - 1);
// Recycle no-longer-visible pages
for (ImageScrollView *page in visiblePages) {
if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
[recycledPages addObject:page];
[page removeFromSuperview];
}
}
[visiblePages minusSet:recycledPages];
// add missing pages
for (int index = firstNeededPag开发者_如何学运维eIndex; index <= lastNeededPageIndex; index++) {
if (![self isDisplayingPageForIndex:index]) {
ImageScrollView *page = [self dequeueRecycledPage];
if (page == nil) {
page = [[[ImageScrollView alloc] init] autorelease];
}
[self configurePage:page forIndex:index];
[pagingScrollView addSubview:page];
[visiblePages addObject:page];
}
}
}
I got it working!
The problem is caused by the navigation controller which doesn't react well with the PhotoScroller sample app! to resolve it make the scrollview a subview of a standard UIView like:
UIView *view = [[UIView alloc] initWithFrame:pagingScrollViewFrame];
[view addSubview:pagingScrollView];
self.view = view;
Thanks :)
I am not totally sure what the problem is as you have described it. So my understanding of the issue you are seeing:
When you click on the table view you are calling your skipToPage message, setting the content offset for the scroll view correctly and the correct page is shown?
When you tap the view it jumps to the first page?
If you could clarify this sentence that might help:
except the jump to page method which when I touch the screen brings the scroll view to the first page UNLESS I scroll to the next page.
- What is the state of the app when you "touch the screen"?
- Are you touching the table view and then calling skipToPage?
- Is the tableview a subview of the scrollview on the first page?
So assuming this is what is happening (please correct me if I have missed something). Could you check what you are doing in the following messages (and post code):
(void)scrollViewDidScroll:(UIScrollView *)sender;
(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
If you were adjusting the content offset in those messages you might get unexpected behaviour from the scroll view.
精彩评论