一、UIRefreshControl控件
UIRefreshControl控件的继承关系:UIRefreshControl:UIControl:UIView:UIResponder:NSObject
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
scrollView.backgroundColor = [UIColor greenColor];
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+100);
[self.view addSubview:scrollView];
/**
1、初始化。注意UIRefreshControl必须加载到ScrollView及其子类中,所以先创建了一个scrollView
- init //控件的大小已经固定了
*/
UIRefreshControl *refresh = [[UIRefreshControl alloc]init];
[scrollView addSubview:refresh];
//添加点击事件
[refresh addTarget:self action:@selector(refreshControlAction:) forControlEvents:UIControlEventValueChanged];
/**
2、配置控件的属性
.tintColor //控件颜色
.attributedTitle //属性字符串
*/
refresh.tintColor = [UIColor redColor];
refresh.attributedTitle = [[NSMutableAttributedString alloc]initWithString:@"下拉刷新"];
/**
3、管理refresh控件的状态
- beginRefreshing //开始刷新
- endRefreshing //结束刷新
.refreshing //是否正在刷新 (只读属性)
*/
}
-(void)refreshControlAction:(UIRefreshControl *)refresh
{
refresh.attributedTitle = [[NSMutableAttributedString alloc]initWithString:@"努力加载中..."];
//这部分逻辑是:睡两秒结束刷新。实际开发中,这里写数据请求的方法,当数据请求成功或失败后都要停止刷新
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSThread sleepForTimeInterval:2];
dispatch_async(dispatch_get_main_queue(), ^{
[refresh endRefreshing]; //结束刷新
refresh.attributedTitle = [[NSMutableAttributedString alloc]initWithString:@"下拉刷新"];
});
});
}