UIPageControl setCurrentPage - UICollectionView iOS6

I'm new to Xcode, so I need your help. I use UIPageControl to show which cell of my View collection is now visible.

The problem is to get a visible cell:

I think in this method

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 

I need to refresh the current page. But how do I get the current page?

Thank you for your help!: -)

+4
source share
2 answers

A page can be defined as the relationship between offset and size.

 - (NSInteger)horizontalPageNumber:(UIScrollView *)scrollView { CGPoint contentOffset = scrollView.contentOffset; CGSize viewSize = scrollView.bounds.size; NSInteger horizontalPage = MAX(0.0, contentOffset.x / viewSize.width); // Here how vertical would work... //NSInteger verticalPage = MAX(0.0, contentOffset.y / viewSize.height); return horizontalPage; } 

There are several ways to cause this. You can do work on each scrollViewDidScroll, but this is a bit redundant. It is best to start it when the drag is completed and further braking does not stop or when braking ends as follows:

 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { self.page.currentPage = [self horizontalPageNumber:scrollView]; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { if (!decelerate) self.page.currentPage = [self horizontalPageNumber:scrollView]; } 
+11
source

If you use a pager, then there is a code that will help you:

 - (IBAction)pagerValueChanged { NSData* imgData = [images objectAtIndex:pager.currentPage]; UIImage* img = [[UIImage alloc]initWithData:imgData]; imageView.image = img; //NSLog(@"img width: %f, img height: %f", img.size.width, img.size.height); [img release]; } 
0
source

All Articles