适用于iOS6之后的苹果提供的下拉刷新

一:iOS6.0及以后:

  • 下拉刷新控件UIRefreshControl
  • TableView属性:refreshControl

二:使用

 1 - (void)colseTheTB
 2 {
 3     [self dismissViewControllerAnimated:YES completion:nil];
 4 }
 5 
 6 - (void)viewDidLoad
 7 {
 8     [super viewDidLoad];
 9 
10     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:@selector(colseTheTB)];
11     
12     //数据源
13     self.dataArray = [[NSMutableArray alloc]initWithCapacity:10];
14     for (int i = 0; i < 10; i ++) {
15         [_dataArray addObject:[NSString stringWithFormat:@"%d",i]];
16     }
17     
18     
19     //适用于 iOS6 之后,系统自带的下拉刷新控件 UIRefreshControl
20     UIRefreshControl *osRefresh = [[UIRefreshControl alloc]init];
21     osRefresh.tintColor = [UIColor lightGrayColor];
22     osRefresh.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
23     [osRefresh addTarget:self action:@selector(doPullRefresh:) forControlEvents:UIControlEventValueChanged];
24     self.refreshControl = osRefresh;
25 
26 }
27 
28 - (void)doPullRefresh:(UIRefreshControl *)refresh
29 {
30     if (refresh.refreshing) {
31         refresh.attributedTitle = [[NSAttributedString alloc]initWithString:@"正在刷新"];
32         [self performSelector:@selector(handleTheRefresh) withObject:nil afterDelay:2];
33     }
34     
35     else
36     {
37         refresh.attributedTitle = [[NSAttributedString alloc]initWithString:@"释放刷新"];
38 
39     }
40 }
41 
42 - (void)handleTheRefresh
43 {
44     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
45     [formatter setDateFormat:@"MMM d, h:mm:ss a"];
46     NSString *lastUpdated = [NSString stringWithFormat:@"时间:%@", [formatter stringFromDate:[NSDate date]]];
47     self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated] ;
48     
49     static int num = 0;
50     num--;
51     [_dataArray insertObject:[NSString stringWithFormat:@"%d",num] atIndex:0];
52     
53     [self.refreshControl endRefreshing];
54     [self.tableView reloadData];
55 }

 三:显示情况

  • 在iOS6上显示情况,请参见 qq for iPhone版本 app
  • 在iOS7 显示情况,是菊花动画,一片一片的铺满
原文地址:https://www.cnblogs.com/cocoajin/p/3490632.html