[iOS]下拉刷新的简单源码(适用于UICollectionView)
2013-12-07 14:30
393 查看
两个源码文件:
1.PullRefreshTableViewController.h
//
// PullRefreshTableViewController.h
// Plancast
//
// Created by Leah Culver on 7/2/10.
// Copyright (c) 2010 Leah Culver
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#import <UIKit/UIKit.h>
@interface PullRefreshTableViewController : UIViewController {
UIView *refreshHeaderView;
UILabel *refreshLabel;
UIImageView *refreshArrow;
UIActivityIndicatorView *refreshSpinner;
BOOL isDragging;
BOOL isLoading;
NSString *textPull;
NSString *textRelease;
NSString *textLoading;
}
@property (nonatomic, strong) UIView *refreshHeaderView;
@property (nonatomic, strong) UILabel *refreshLabel;
@property (nonatomic, strong) UIImageView *refreshArrow;
@property (nonatomic, strong) UIActivityIndicatorView *refreshSpinner;
@property (nonatomic, strong) NSString *textPull;
@property (nonatomic, strong) NSString *textRelease;
@property (nonatomic, strong) NSString *textLoading;
@property (nonatomic, strong) UICollectionView *myRefreshview;
- (void)setupStrings;
- (void)addPullToRefreshHeader;
- (void)startLoading;
- (void)stopRefreshing;
- (void)refresh;
@end
2.PullRefreshTableViewController.m
//
// PullRefreshTableViewController.m
// Plancast
//
// Created by Leah Culver on 7/2/10.
// Copyright (c) 2010 Leah Culver
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#import <QuartzCore/QuartzCore.h>
#import "PullRefreshTableViewController.h"
#define REFRESH_HEADER_HEIGHT 52.0f
@implementation PullRefreshTableViewController
@synthesize textPull, textRelease, textLoading, refreshHeaderView, refreshLabel, refreshArrow, refreshSpinner;
-(void)viewDidLoad{
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated{
[self addPullToRefreshHeader];
[self setupStrings];
[self.myRefreshview setAlwaysBounceVertical:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self != nil) {
[self setupStrings];
}
return self;
}
- (void)setupStrings{
textPull = @"下拉刷新...";
textRelease = @"松开刷新...";
textLoading = @"加载中...";
}
- (void)addPullToRefreshHeader {
refreshHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0 - REFRESH_HEADER_HEIGHT, 320, REFRESH_HEADER_HEIGHT)];
refreshHeaderView.backgroundColor = [UIColor clearColor];
refreshLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, REFRESH_HEADER_HEIGHT)];
refreshLabel.backgroundColor = [UIColor clearColor];
refreshLabel.font = [UIFont boldSystemFontOfSize:12.0];
refreshLabel.textAlignment = NSTextAlignmentCenter;
refreshArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];
refreshArrow.frame = CGRectMake(floorf((REFRESH_HEADER_HEIGHT - 27) / 2),
(floorf(REFRESH_HEADER_HEIGHT - 44) / 2),
27, 44);
refreshSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
refreshSpinner.frame = CGRectMake(floorf(floorf(REFRESH_HEADER_HEIGHT - 20) / 2), floorf((REFRESH_HEADER_HEIGHT - 20) / 2), 20, 20);
refreshSpinner.hidesWhenStopped = YES;
[refreshHeaderView addSubview:refreshLabel];
[refreshHeaderView addSubview:refreshArrow];
[refreshHeaderView addSubview:refreshSpinner];
[self.myRefreshview addSubview:refreshHeaderView];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if (isLoading) return;
isDragging = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (isLoading) {
// Update the content inset, good for section headers
if (scrollView.contentOffset.y > 0)
self.myRefreshview.contentInset = UIEdgeInsetsZero;
else if (scrollView.contentOffset.y >= -REFRESH_HEADER_HEIGHT)
self.myRefreshview.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (isDragging && scrollView.contentOffset.y < 0) {
// Update the arrow direction and label
[UIView animateWithDuration:0.25 animations:^{
if (scrollView.contentOffset.y < -REFRESH_HEADER_HEIGHT) {
// User is scrolling above the header
refreshLabel.text = self.textRelease;
[refreshArrow layer].transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
} else {
// User is scrolling somewhere within the header
refreshLabel.text = self.textPull;
[refreshArrow layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);
}
}];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (isLoading) return;
isDragging = NO;
if (scrollView.contentOffset.y <= -REFRESH_HEADER_HEIGHT) {
// Released above the header
[self startLoading];
}
}
- (void)startLoading {
isLoading = YES;
// Show the header
[UIView animateWithDuration:0.3 animations:^{
self.myRefreshview.contentInset = UIEdgeInsetsMake(REFRESH_HEADER_HEIGHT, 0, 0, 0);
refreshLabel.text = self.textLoading;
refreshArrow.hidden = YES;
[refreshSpinner startAnimating];
}];
// Refresh action!
[self refresh];
}
- (void)stopRefreshing {
isLoading = NO;
// Hide the header
[UIView animateWithDuration:0.3 animations:^{
self.myRefreshview.contentInset = UIEdgeInsetsZero;
[refreshArrow layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);
}
completion:^(BOOL finished) {
[self performSelector:@selector(stopLoadingComplete)];
}];
}
- (void)stopLoadingComplete {
// Reset the header
refreshLabel.text = self.textPull;
refreshArrow.hidden = NO;
[refreshSpinner stopAnimating];
}
- (void)refresh {
// This is just a demo. Override this method with your custom reload action.
// Don't forget to call stopLoading at the end.
[self performSelector:@selector(stopRefreshing) withObject:nil afterDelay:2.0];
}
@end
在引用的时候,继承自上面的类,同时在头文件声明且在xib或storyboard中链接:
@property (weak, nonatomic) IBOutlet UICollectionView *faxsView;
重写父类中的refresh函数:
- (void)refresh {
[self performSelector:@selector(addItem) withObject:nil afterDelay:2.0];
[super refresh];
}
- (void)addItem {
NSLog(@"!!!!!!!!!!!!!!!!!!");
}
1.PullRefreshTableViewController.h
//
// PullRefreshTableViewController.h
// Plancast
//
// Created by Leah Culver on 7/2/10.
// Copyright (c) 2010 Leah Culver
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#import <UIKit/UIKit.h>
@interface PullRefreshTableViewController : UIViewController {
UIView *refreshHeaderView;
UILabel *refreshLabel;
UIImageView *refreshArrow;
UIActivityIndicatorView *refreshSpinner;
BOOL isDragging;
BOOL isLoading;
NSString *textPull;
NSString *textRelease;
NSString *textLoading;
}
@property (nonatomic, strong) UIView *refreshHeaderView;
@property (nonatomic, strong) UILabel *refreshLabel;
@property (nonatomic, strong) UIImageView *refreshArrow;
@property (nonatomic, strong) UIActivityIndicatorView *refreshSpinner;
@property (nonatomic, strong) NSString *textPull;
@property (nonatomic, strong) NSString *textRelease;
@property (nonatomic, strong) NSString *textLoading;
@property (nonatomic, strong) UICollectionView *myRefreshview;
- (void)setupStrings;
- (void)addPullToRefreshHeader;
- (void)startLoading;
- (void)stopRefreshing;
- (void)refresh;
@end
2.PullRefreshTableViewController.m
//
// PullRefreshTableViewController.m
// Plancast
//
// Created by Leah Culver on 7/2/10.
// Copyright (c) 2010 Leah Culver
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#import <QuartzCore/QuartzCore.h>
#import "PullRefreshTableViewController.h"
#define REFRESH_HEADER_HEIGHT 52.0f
@implementation PullRefreshTableViewController
@synthesize textPull, textRelease, textLoading, refreshHeaderView, refreshLabel, refreshArrow, refreshSpinner;
-(void)viewDidLoad{
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated{
[self addPullToRefreshHeader];
[self setupStrings];
[self.myRefreshview setAlwaysBounceVertical:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self != nil) {
[self setupStrings];
}
return self;
}
- (void)setupStrings{
textPull = @"下拉刷新...";
textRelease = @"松开刷新...";
textLoading = @"加载中...";
}
- (void)addPullToRefreshHeader {
refreshHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0 - REFRESH_HEADER_HEIGHT, 320, REFRESH_HEADER_HEIGHT)];
refreshHeaderView.backgroundColor = [UIColor clearColor];
refreshLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, REFRESH_HEADER_HEIGHT)];
refreshLabel.backgroundColor = [UIColor clearColor];
refreshLabel.font = [UIFont boldSystemFontOfSize:12.0];
refreshLabel.textAlignment = NSTextAlignmentCenter;
refreshArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];
refreshArrow.frame = CGRectMake(floorf((REFRESH_HEADER_HEIGHT - 27) / 2),
(floorf(REFRESH_HEADER_HEIGHT - 44) / 2),
27, 44);
refreshSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
refreshSpinner.frame = CGRectMake(floorf(floorf(REFRESH_HEADER_HEIGHT - 20) / 2), floorf((REFRESH_HEADER_HEIGHT - 20) / 2), 20, 20);
refreshSpinner.hidesWhenStopped = YES;
[refreshHeaderView addSubview:refreshLabel];
[refreshHeaderView addSubview:refreshArrow];
[refreshHeaderView addSubview:refreshSpinner];
[self.myRefreshview addSubview:refreshHeaderView];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if (isLoading) return;
isDragging = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (isLoading) {
// Update the content inset, good for section headers
if (scrollView.contentOffset.y > 0)
self.myRefreshview.contentInset = UIEdgeInsetsZero;
else if (scrollView.contentOffset.y >= -REFRESH_HEADER_HEIGHT)
self.myRefreshview.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (isDragging && scrollView.contentOffset.y < 0) {
// Update the arrow direction and label
[UIView animateWithDuration:0.25 animations:^{
if (scrollView.contentOffset.y < -REFRESH_HEADER_HEIGHT) {
// User is scrolling above the header
refreshLabel.text = self.textRelease;
[refreshArrow layer].transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
} else {
// User is scrolling somewhere within the header
refreshLabel.text = self.textPull;
[refreshArrow layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);
}
}];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (isLoading) return;
isDragging = NO;
if (scrollView.contentOffset.y <= -REFRESH_HEADER_HEIGHT) {
// Released above the header
[self startLoading];
}
}
- (void)startLoading {
isLoading = YES;
// Show the header
[UIView animateWithDuration:0.3 animations:^{
self.myRefreshview.contentInset = UIEdgeInsetsMake(REFRESH_HEADER_HEIGHT, 0, 0, 0);
refreshLabel.text = self.textLoading;
refreshArrow.hidden = YES;
[refreshSpinner startAnimating];
}];
// Refresh action!
[self refresh];
}
- (void)stopRefreshing {
isLoading = NO;
// Hide the header
[UIView animateWithDuration:0.3 animations:^{
self.myRefreshview.contentInset = UIEdgeInsetsZero;
[refreshArrow layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);
}
completion:^(BOOL finished) {
[self performSelector:@selector(stopLoadingComplete)];
}];
}
- (void)stopLoadingComplete {
// Reset the header
refreshLabel.text = self.textPull;
refreshArrow.hidden = NO;
[refreshSpinner stopAnimating];
}
- (void)refresh {
// This is just a demo. Override this method with your custom reload action.
// Don't forget to call stopLoading at the end.
[self performSelector:@selector(stopRefreshing) withObject:nil afterDelay:2.0];
}
@end
在引用的时候,继承自上面的类,同时在头文件声明且在xib或storyboard中链接:
@property (weak, nonatomic) IBOutlet UICollectionView *faxsView;
重写父类中的refresh函数:
- (void)refresh {
[self performSelector:@selector(addItem) withObject:nil afterDelay:2.0];
[super refresh];
}
- (void)addItem {
NSLog(@"!!!!!!!!!!!!!!!!!!");
}
相关文章推荐
- iOS UICollectionViewController基本使用方法和简单的相册实现
- iOS[正解] - 在UICollectionViewCell中使用下拉刷新,无法出现刷新条
- IOS简单实现瀑布流UICollectionView
- iOS 使用MJRefrsh上拉加载,下拉刷新cell不充满UICollectionView时不能刷新的问题
- iOS UICollectionView简单使用
- iOS UICollectionView学习之一,UICollectionView + storyboard 简单应用
- iOS UICollectionView简单使用
- iOS流水布局UICollectionView简单使用
- IOS 支付宝-五福简单框架实现-线性滚动(UICollectionView)
- IOS-给UIScrollView(包括继承它的UITableView、UICollectionView)添加下拉刷新-上拉加载更多
- 一、ios的通过Xib构建UICollectionView的简单运用
- iOS学习笔记6- iOS UICollectionView简单使用
- iOS开发笔记之UICollectionView的简单使用
- iOS UICollectionView简单使用
- ios的通过Xib构建UICollectionView的简单运用
- iOS UICollectionView简单使用
- iOS UICollectionView简单使用
- IOS基金会_ UICollectionView简单易用
- iOS UICollectionView简单使用
- IOS基础_ UICollectionView的简单使用