而contentSize、contentInset和contentOffset 是 scrollView三个基本的属性。


  • contentSize: The size of the content view.
  • 其实就是scrollview可以滚动的区域,比如frame = (0 ,0 ,320 ,480) contentSize = (320 ,960),代表你的scrollview可以上下滚动,滚动区域为frame大小的两倍。
  • contentOffset:The point at which the origin of the content view is offset from the origin of the scroll view.
  • 是scrollview当前显示区域顶点相对于frame顶点的偏移量,比如上个例子你拉到最下面,contentoffset就是(0 ,480),也就是y偏移了480
  • contentInset:The distance that the content view is inset from the enclosing scroll view.
  • 是scrollview的contentview的顶点相对于scrollview的位置,例如你的contentInset = (0 ,100),那么你的contentview就是从scrollview的(0 ,100)开始显示

动画效果


image

pragma mark - scroll 下拉刷新效果

- (void)setSrollViewDownFlash{
    [self.view addSubview:self.voiceScrollView];
    self.view.backgroundColor = [UIColor blackColor];
    self.view.frame = CGRectMake(0, 0, PKScreenW, PKScreenH);
    self.voiceScrollView.backgroundColor = [UIColor yellowColor];
    self.voiceScrollView.contentSize = CGSizeMake(PKScreenW/2, PKScreenH);
    //上,下边界
    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, PKScreenW/2, 2)];
    UIView *v0 = [[UIView alloc]initWithFrame:CGRectMake(0, PKScreenH-2, PKScreenW/2, 2)];
    v.backgroundColor = [UIColor blueColor];
    v0.backgroundColor = [UIColor blueColor];
    [self.voiceScrollView addSubview:v];
    [self.voiceScrollView addSubview:v0];
    //标题
    UILabel *v2 = [[UILabel alloc] init];//initWithFrame:CGRectMake(0, -10, PKScreenW/2, 10)];
    v2.text = @"123";
    [self.voiceScrollView addSubview:v2];
    //位置
    [self.voiceScrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(self.view.mas_top);
        make.size.equalTo(CGSizeMake(PKScreenW/2, self.view.height/2));
    }];
    //字符
    [v2 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.voiceScrollView.mas_top);
        make.centerX.equalTo(self.voiceScrollView);
    }];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //上拉刷新
    CGFloat f = scrollView.contentOffset.y;
    if (f > PKScreenH/2) {
        PKLog(@"up load page");
    }
    //下拉刷新
    if (f < - 40.0f) {
        PKLog(@"down load page");
    }
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    CGFloat f = scrollView.contentOffset.y;
     //上拉刷新
    if (f > PKScreenH/2) {
        [UIView animateWithDuration:0.4 animations:^{
            [self.voiceScrollView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 20.0f, 0.0f)];
        }];
        [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(func) userInfo:nil repeats:false];
    }
    
    //下拉刷新
    if (f < - 40.0f) {
        [UIView animateWithDuration:0.4 animations:^{
            [self.voiceScrollView setContentInset:UIEdgeInsetsMake(20.0f, 0.0f, 0.0f, 0.0f)];
        }];
        [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(func) userInfo:nil repeats:false];
    }
    PKLog(@"%f",f);
}
- (void)func{
    [UIView animateWithDuration:0.4 animations:^{
        [self.voiceScrollView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
    }];
}