码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-05-07 16:26:45      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

- (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);
        }];

    });

}

 

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

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!