UIScrollView视图端口坐标

时间:2016-09-07 07:10:23

标签: ios objective-c uiscrollview uiscrollviewdelegate

这就是我在UIScrollView

中添加viewDidLoad的方法
    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; 
scrollView.backgroundColor = [UIColor redColor]; 
scrollView.scrollEnabled = YES; 
scrollView.pagingEnabled = YES; 
scrollView.showsVerticalScrollIndicator = YES; 
scrollView.showsHorizontalScrollIndicator = YES; 
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width , self.view.bounds.size.height * 2); 
[self.view addSubview:scrollView]; 
scrollView.delegate = self;

第一次加载scrollView时,它的坐标为:{(0,0), (widthOfiPhone, 0), (0, heightOfiPhone), (widthOfiPhone, heightOfiPhone)}

每当滚动完成,我想在scrollview中获得视口的坐标。这该怎么做?感谢

编辑: From the image, As the view is scrolling, it's y co-ordinates are changing. How do I track that

图片来源:https://oleb.net/blog/2014/04/understanding-uiscrollview/

2 个答案:

答案 0 :(得分:0)

您可以实施UIScrollView delegate方法来检测滚动结束的时间:scrollViewDidEndDecelerating,在此方法中,您可以获得scrollviewcontent的坐标。顺便说一句content's cooridinate与尊重scrollview !!!相同!

您可以从滚动视图中获取visible rect

  CGRect visibleRect = [subViewOfScrollView convertRect:subViewOfScrollView.bounds toView:self.view];

答案 1 :(得分:0)

首先,观察scrollview代理,然后实现其代理方法

(void) scrollViewDidEndDecelerating: (UIScrollView *) scrollView {
    NSLog (@ "% f,% f,% f,% f", scrollView.frame.origin.x,
    scrollView.frame.origin.y,
    scrollView.frame.size.width,
    scrollView.frame.size.height);
}

这应该是可能的