UIScrollView及其子视图的坐标

UIScrollView的Frame控制的是用户可以滑动的区域。

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, 320, 200);

scrollView.contentSize = CGSizeMake(800, 200);

其子View的Frame是基于UIScrollView的contentSize的。

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200); [scrollView addSubView:imageView];

UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 200, 200);[scrollView addSubView:imageView2];

UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 400, 200, 200);[scrollView addSubView:imageView3];

UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 600, 200, 200);[scrollView addSubView:imageView4];

也可以先将子View添加到scrollView中,再设置子View的frame,最后设置scrollView的contentSize。

滑动scrollView时,scrollView和子View的Frame不会改变,改变的是scrollView的contentOffset。

scrollView的子View在scrollView的父视图中的坐标可用如下方法计算得出:

CGRect rect = [scrollView convertRect:imageView.frame toView:scrollView.superView];

你可能感兴趣的:(UIScrollView及其子视图的坐标)