画面上部から画面を下に引っ張ると、データを読み込むといったUIがiPhoneには多いかと思います。
あれを実現するために、iOS6から追加された「UIRefreshControl」を使います。
これが簡単で、UIRefreshControlのインスタンスをつくり、UIScrollViewのプロパティにセットすればOKです。
UIScrollViewなので、UITableViewControllerでもUIWebViewでもできます。
@interface RefreshTestController () @property(nonatomic) UIRefreshControl *refreshControl;
-(void)viewDidLoad
{
[super viewDidLoad];
//インスタンス生成
self.refreshControl = [[UIRefreshControl alloc] init];
//引っ張っているときに、表示させる文字
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
NSAttributedString *title = [[NSAttributedString alloc] initWithString:@"下に引っ張って更新" attributes:attributes];
self.refreshControl.attributedTitle = title;
//色を変更
self.refreshControl.tintColor = [UIColor orangeColor];
//イベントを関連づけ
[self.refreshControl addTarget:self action:@selector(onRefresh:) forControlEvents:UIControlEventValueChanged];
//セット
[self.collectionView addSubview: self.refreshControl];
}
引っ張ったときに呼び出されるメソッド
-(void)onRefresh:(id)sender
{
//画面上部で、くるくる回ります。
[self.refreshControl beginRefreshing];
//更新メソッド
//くるくるが停止します。
[self.refreshControl endRefreshing];
}
これを使って、プレゼントをキュレーションするアプリを開発しました。
インストールして、動作確認をしてみてください。
True Gift
