4

我想知道 UIScrollView 中当前滚动页面的索引。我搜索了许多网站,没有任何帮助。欢迎任何建议。

4

4 回答 4

7

尝试这个

    //Horizontal
    NSInteger pagenumber = scrollView.contentOffset.x / scrollView.bounds.size.width;
    //Vertical
    NSInteger pagenumber = scrollView.contentOffset.y / scrollView.bounds.size.height;
于 2012-10-05T13:58:53.760 回答
6

对于那些需要代码(swift)的人, UIScrollViewDelegate 提供了方法,这使用了上面的水平示例

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    print("END Scrolling \(scrollView.contentOffset.x / scrollView.bounds.size.width)")
    index = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
}
于 2016-08-04T23:29:59.400 回答
0

借助一些数学计算,您可以获得页面的索引号,我发送的代码仅供参考。我已经做好了。

-(void)KinarafindingSelectedCategory:(UIScrollView*)scrollView{
    for (UIView *v in scrollView.subviews)
    {
        if ([v isKindOfClass:[UIButton class]])
        {
            UIButton *btn=(UIButton*)v;

            float x1=scrollView.contentOffset.x+(([UIScreen mainScreen].bounds.size.width/2)-(btn.frame.size.width/2));
            float x2=scrollView.contentOffset.x+(([UIScreen mainScreen].bounds.size.width/2)+(btn.frame.size.width/2));
            float BtnMidPoint=btn.frame.origin.x+(btn.frame.size.width/2);
            if(BtnMidPoint >= x1 && BtnMidPoint <= x2)
            {
                if(scrollView==KinaraCategory)
                {
                    KinaraSelectedCategoryName=btn.titleLabel.text;
                    KinaraSelectedCategoryID=btn.tag;
                    NSLog(@"Selected Category Tag : %d",KinaraSelectedCategoryID);
                    NSLog(@"Selected Category Name : %@",KinaraSelectedCategoryName);
                }
                else if(scrollView==KinaraSubCategory)
                {
                    KinaraSelectedSubCategoryID=btn.tag;
                    KinaraSelectedSubCategoryName=btn.titleLabel.text;

                    NSLog(@"Selected SubCategory Tag : %d",KinaraSelectedSubCategoryID);
                    NSLog(@"Selected SubCategory Name : %@",KinaraSelectedSubCategoryName);
                }
                break;
            }
        }
    }

}
于 2012-10-05T11:00:30.297 回答
0

使用该contentOffset属性,它返回 a CGSize,它是UIScrollView.

于 2012-10-05T10:50:21.470 回答