自定义刷新控件的实现原理

- (void)viewDidLoad {
    [super viewDidLoad];
  
    UIButton *button  = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, -30, self.view.bounds.size.width, 30);
    [button setTitle:@"下拉刷新" forState:UIControlStateNormal];
    [self.view addSubview:button];
    button.backgroundColor = [UIColor redColor];
    self.refreshButton = button;
    [self.tableView addSubview:button];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSLog(@"%f",scrollView.contentOffset.y);
    
    if (scrollView.contentInset.top == 30) 
        return;

    if (scrollView.contentOffset.y <= -30) {
        
        [self.refreshButton setTitle:@"松开刷新" forState:UIControlStateNormal];
        
    }
    else{
        [self.refreshButton setTitle:@"下拉刷新" forState:UIControlStateNormal];
    }
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (scrollView.contentOffset.y <= -30) {
       
        
        [UIView animateWithDuration:0.5 animations:^{
             [self.refreshButton setTitle:@"正在刷新" forState:UIControlStateNormal];
            scrollView.contentInset = UIEdgeInsetsMake(30, 0, 0, 0);
        }];
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.5 animations:^{
            
            scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        }];

    });

}
原文地址:https://www.cnblogs.com/yintingting/p/5468489.html