2014-01-28 16 views
5

다음 코드를 사용하여 가로 방향으로 collection view 페이지가 매겨졌습니다. 내가 가지고 얼마나 많은 페이지와 현재 페이지를 표시하기 위해 pageControl을 사용하고UICollectionView 현재 페이지를 가져올 수 없습니다.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.collectionView.pagingEnabled = YES; 
    self.collectionView.directionalLockEnabled = YES; 
    self.collectionView.bounces = NO; 
    self.collectionView.showsHorizontalScrollIndicator = NO; 
    self.collectionView.delegate = self; 

    [self setup]; 

    self.pageControl.currentPage = 0; 

    self.pageControl.numberOfPages = floor(self.collectionView.contentSize.width/ self.collectionView.frame.size.width) + 1; 
} 

.

현재 페이지를 확인하기 위해 다음 링크와 비슷한 방법을 사용했습니다.

iOS6 UICollectionView and UIPageControl - How to get visible cell?

다음으로

UICollectionView: current index path for page control

.

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 
    NSInteger currentPage = self.collectionView.contentOffset.x/self.collectionView.frame.size.width; 
    self.pageControl.currentPage = currentPage; 
} 

그러나 작동하지 않습니다. collection view을 다음 페이지로 스 와이프하면 pageControl은 여전히 ​​첫 번째 페이지를 가리 킵니다. 그것의 가치는 변하지 않습니다.

self.collectionView.contentOffset.x 값을 인쇄했는데 항상 프레임 안쪽에 인쇄됩니다 (첫 페이지는 다음 페이지로 스 와이프 한 것입니다). 다음 페이지로 이동하지 않습니다.

내가 잘못 했습니까?

답변

5

세포 사이의 간격을 계산하고 있습니까?

이 시도 :

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 
    float currentPage = self.collectionView.contentOffset.x/self.collectionView.frame.size.width; 
    self.pageControl.currentPage = ceil(currentPage); 

    NSLog(@"Values: %f %f %f", self.collectionView.contentOffset.x, self.collectionView.frame.size.width, ceil(currentIndex)); 
} 
관련 문제