简体   繁体   English

UIPageControl setCurrentPage-UICollectionView iOS6

[英]UIPageControl setCurrentPage - UICollectionView iOS6

I'm new to XCode so I need your help please. 我是XCode的新手,所以我需要您的帮助。 I use a UIPageControl to show which cell of my collection View is now visible. 我使用UIPageControl来显示集合视图的哪个单元格现在可见。

The problem is to get the visible cell: 问题是获取可见的单元格:

I think in this Method 我认为在这种方法

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

I should update the current Page. 我应该更新当前页面。 But how do I get the current Page? 但是,如何获取当前页面?

Thanks for your help! 谢谢你的帮助! :-) :-)

The page can be defined as the ratio between the offset and the size. 可以将页面定义为偏移量与尺寸之间的比率。

- (NSInteger)horizontalPageNumber:(UIScrollView *)scrollView {
    CGPoint contentOffset = scrollView.contentOffset;
    CGSize viewSize = scrollView.bounds.size;

    NSInteger horizontalPage = MAX(0.0, contentOffset.x / viewSize.width);

    // Here's how vertical would work...
    //NSInteger verticalPage = MAX(0.0, contentOffset.y / viewSize.height);

    return horizontalPage;
}

There are a couple ways to trigger this. 有两种触发方式。 You can do the work on every scrollViewDidScroll, but that's a bit excessive. 您可以在每个scrollViewDidScroll上完成工作,但这有点多余。 A better way is to run it either when dragging is complete and there will be no further deceleration, or when deceleration 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];
}

If you use the pager, than is is the code should 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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM