xcode ios:如何获取当前的索引? UIscrollView 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12744622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ios: how to get index of current ?UIscrollView page
提问by user1564754
I want to know index of current scrolled page in UIScrollView. I have searched through many sites by none helps. Any suggestions are welcome.
我想知道 UIScrollView 中当前滚动页面的索引。我已经搜索了许多网站,但没有任何帮助。欢迎任何建议。
回答by Lithu T.V
Try this
尝试这个
//Horizontal
NSInteger pagenumber = scrollView.contentOffset.x / scrollView.bounds.size.width;
//Vertical
NSInteger pagenumber = scrollView.contentOffset.y / scrollView.bounds.size.height;
回答by Mindeater
For those needing code (swift) , the UIScrollViewDelegate provides the method, this uses the horizontal example from above
对于那些需要代码 (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)
}
回答by craigmj
Use the contentOffset
property, which returns a CGSize
that is the offset of the content in the UIScrollView
.
使用contentOffset
属性,它返回CGSize
.a 中内容的偏移量UIScrollView
。
回答by Mohd Kalimullah Sheikh
with the help of some mathematical calculation you can get index number of page I am sending code for reference only . I have done.
在一些数学计算的帮助下,您可以获得页面的索引号我发送的代码仅供参考。我已经做好了。
-(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;
}
}
}
}