UIScrollView的判断位置的属性如下:
- contentSize:CGSize类型,scrollview可以滑动的区域,例如,一个view的frame为(0,0,320,480),而scrollview的contentSize为(320,1080),则表示scrollView的垂直滑动区域为整个屏幕的3倍。
- contentView:scrollview的内容显示区域,一般情况下和scrollview的contentsize保持一致。
- contentOffset:CGPoint类型,它表示scrollView当前的显示区域顶点相对于frame顶点的偏移量,如在上面的例子中,如果某一时刻contentOffset的值为(0,960),就表示scrollview的偏移量为(0,960)。
- contentInset:UIEdgeInsets类型,srollciew的contentView的顶点相对于scrollview的位置,它标识了contenView开始显示的位置,这个属性和css以及xaml中的margin属性类似。
-
- 知道了上面几个概念后,要判断srollciew是否滑到底部就容易多了,代码如下:
当currentOffset与maximumOffset的值相等时,即说明scrollview已经滑到底部了。CGPoint offset = scrollView.contentOffset; CGRect bounds = scrollView.bounds; CGSize size = scrollView.contentSize; UIEdgeInsets inset = scrollView.contentInset; CGFloat currentOffset = offset.y + bounds.size.height – inset.bottom; CGFloat maximumOffset = size.height;
-
同理,还可以根具上述两个偏移量的差值实现当滑到某一区域时,做点其它什么事,比如当滑到离底部还有50个像素的时候,在后台加载更多的数据:
if((maximumOffset – currentOffset)<50.0)
NSLog(@”LoadMore…”);
#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIScrollView *srcollView; @property (retain, nonatomic) NSMutableArray* imageArray; @property (retain, nonatomic) UIImageView *imageView; @property (weak, nonatomic) IBOutlet UIPageControl *pageControl; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self.srcollView setContentSize:CGSizeMake(320 * 3, 0)]; self.srcollView.backgroundColor = [UIColor redColor]; self.imageArray = [[NSMutableArray alloc] initWithObjects: [UIImage imageNamed:@"1.jpg"], [UIImage imageNamed:@"2.png"], [UIImage imageNamed:@"3.jpg"],nil]; self.srcollView.showsHorizontalScrollIndicator = YES; self.srcollView.pagingEnabled = YES; self.srcollView.delegate = self; self.srcollView.bounces = NO; // 是够可以显示边界区域, NO为不显示。 for (int i = 0; i < self.imageArray.count; i++) { _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320*i, 0, 320, 320)]; [_imageView setImage:self.imageArray[i]]; [self.srcollView addSubview:_imageView]; } self.pageControl.numberOfPages = self.imageArray.count; [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ NSLog(@"offset = %@", NSStringFromCGPoint(scrollView.contentOffset)); float pageWidth = self.srcollView.frame.size.width; int currentPage = self.srcollView.contentOffset.x / pageWidth; NSLog(@"currentPage = %d", currentPage); self.pageControl.currentPage = currentPage; } - (IBAction)changePage:(UIPageControl*)pageControl{ int currentPage = self.pageControl.currentPage; NSLog(@"call %s, currentPage = %d", __FUNCTION__, currentPage); [self.srcollView setContentOffset:CGPointMake(320 * currentPage, 0) animated:YES]; } @end