Pull to Refresh 拖动刷新之我的实现
项目中需要用到pull to refresh这个功能, 从网上找了下找到了这个:https://github.com/leah/PullToRefresh。
这个代码对我来说有一个很大的问题。其中拖动刷新的实现是放在TableViewControllerL里的,我要的放在
自定义UIView中的实现。经过一番改造终于实现了出来。
首先,我们整个的拖动刷新都是从拖动操作发起的,如下的方法必须予以合适的实现:
拖动相关的了解之后。拖动之前需要在初始化方法里做好准备工作。
设置下什么“拖动获取更多”,“加载中。。。”之类的文字。还有箭头,ActivityIndicator之类的界面元素。
然后就是根据加载的不同状态设置界面语速的文字,位置和隐藏与否。代码:
最后最重要就是加载了。在加载方法中填充必要的代码,或者在子类的加载方法中添加(如果你要继承的话,最好还是继承一下)。示例:
完整代码如下:
.m
这个代码对我来说有一个很大的问题。其中拖动刷新的实现是放在TableViewControllerL里的,我要的放在
自定义UIView中的实现。经过一番改造终于实现了出来。
首先,我们整个的拖动刷新都是从拖动操作发起的,如下的方法必须予以合适的实现:
1 2 3 4 5 6 7 8 9 10 | // 拖动动作开始,如果正在获取更多信息不做任何操作 - ( void )scrollViewWillBeginDragging:(UIScrollView *)scrollView; // 拖动中。根据是否已经开始加载信息设置tableView的contentInset值 - ( void )scrollViewDidScroll:(UIScrollView *)scrollView; // 拖动完成,开始加载。如果上次的加载没有完成,不做任何操作 - ( void )scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:( BOOL )decelerate; |
拖动相关的了解之后。拖动之前需要在初始化方法里做好准备工作。
设置下什么“拖动获取更多”,“加载中。。。”之类的文字。还有箭头,ActivityIndicator之类的界面元素。
然后就是根据加载的不同状态设置界面语速的文字,位置和隐藏与否。代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #pragma mark - #pragma mark Pull to refresh methods - ( void )setUpStrings{ _textPull = [[ NSString alloc]initWithString:@ "拖动获取更多。。。" ]; _textRelease = [[ NSString alloc]initWithString:@ "释放获取更多。。。" ]; _textRelease = [[ NSString alloc]initWithString:@ "加载中。。。" ]; } - ( void )addPullToRefreshHeader{ CGRect mainBounds = [[UIScreen mainScreen] bounds]; _refreshHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0 - REFRESH_HEADER_VIEW, mainBounds.size.width, REFRESH_HEADER_VIEW)]; _refreshHeaderView.backgroundColor = [UIColor clearColor]; _refreshLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, mainBounds.size.width, REFRESH_HEADER_VIEW)]; _refreshLabel.backgroundColor = [UIColor clearColor]; _refreshLabel.font = [UIFont boldSystemFontOfSize:12.f]; _refreshLabel.textAlignment = UITextAlignmentCenter; _refreshArrow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@ "arrow.png" ]]; _refreshArrow.frame = CGRectMake(floorf((REFRESH_HEADER_VIEW - 20) / 2), floorf((REFRESH_HEADER_VIEW - 20) / 2), 27, 44); _refreshSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; _refreshSpinner.frame = CGRectMake(floorf((REFRESH_HEADER_VIEW - 20) / 2), floorf((REFRESH_HEADER_VIEW - 20) / 2), 27, 44); _refreshSpinner.hidesWhenStopped = YES ; [_refreshHeaderView addSubview:_refreshLabel]; [_refreshHeaderView addSubview:_refreshArrow]; [_refreshHeaderView addSubview:_refreshSpinner]; [ self .weiboTableView addSubview:_refreshHeaderView]; } - ( void )startLoading{ _isLoading = YES ; [UIView animateWithDuration:0.3f animations:^{ self .weiboTableView.contentInset = UIEdgeInsetsMake(REFRESH_HEADER_VIEW, 0, 0, 0); _refreshLabel.text = self .textLoading; _refreshArrow.hidden = YES ; [_refreshSpinner startAnimating]; }]; [ self refresh]; } - ( void )stopLoadingComplete { // Reset the header _refreshLabel.text = self .textPull; _refreshArrow.hidden = NO ; [_refreshSpinner stopAnimating]; } - ( void )stopLoading{ _isLoading = NO ; [UIView animateWithDuration:0.3f animations:^{ self .weiboTableView.contentInset = UIEdgeInsetsZero; [_refreshArrow layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1); } completion:^( BOOL finished){ [ self performSelector: @selector (stopLoadingComplete)]; }]; } - ( void )refresh{ [ self performSelector: @selector (stopLoading) withObject: nil afterDelay:5.f]; } |
最后最重要就是加载了。在加载方法中填充必要的代码,或者在子类的加载方法中添加(如果你要继承的话,最好还是继承一下)。示例:
1 2 3 4 5 6 | - ( void )refresh{ // 加载功能代码 // 最后一句必不可少 [ self performSelector: @selector (stopLoading) withObject: nil afterDelay:5.f]; } |
完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | // // ScrollRefreshPageView.h // Sample Project // // Created by Bruce Li (mr_bruce). // QQ: 1828099940 // #import <UIKit/UIKit.h> #import "TSPageView.h" @interface ScrollRefreshPageView : UIView { UIView *_refreshHeaderView; UILabel *_refreshLabel; UIImageView *_refreshArrow; UIActivityIndicatorView *_refreshSpinner; BOOL _isDragging; BOOL _isLoading; NSString *_textPull; NSString *_textRelease; NSString *_textLoading; } @property ( nonatomic , copy ) NSString *textPull; @property ( nonatomic , copy ) NSString *textRelease; @property ( nonatomic , copy ) NSString *textLoading; - ( void )setUpStrings; - ( void )addPullToRefreshHeader; - ( void )startLoading; - ( void )stopLoading; - ( void )refresh; @end |
.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | #import "ScrollRefreshPageView.h" #import <QuartzCore/QuartzCore.h> #define REFRESH_HEADER_VIEW 90.f @implementation ScrollRefreshPageView @synthesize textPull = _textPull; @synthesize textRelease = _textRelease; @synthesize textLoading = _textLoading; - ( id )initWithFrame:(CGRect)frame { self = [ super initWithFrame:frame]; if ( self ) { [ self setUpStrings]; [ self addPullToRefreshHeader]; } return self ; } - ( void )dealloc{ [_refreshHeaderView release]; [_refreshLabel release]; [_refreshArrow release]; [_refreshSpinner release]; [_textPull release]; [_textRelease release]; [_textLoading release]; [ super dealloc]; } #pragma mark - #pragma mark Scroll view delegate - ( void )scrollViewWillBeginDragging:(UIScrollView *)scrollView{ if (_isLoading) { return ; } _isDragging = YES ; } - ( void )scrollViewDidScroll:(UIScrollView *)scrollView{ if (_isLoading) { if (scrollView.contentOffset.y > 0) { self .weiboTableView.contentInset = UIEdgeInsetsZero; } else if (scrollView.contentOffset.y >= -REFRESH_HEADER_VIEW) { self .weiboTableView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0); } } else if (_isDragging && scrollView.contentOffset.y < 0) { [UIView animateWithDuration:0.25f animations:^{ if (scrollView.contentOffset.y < -REFRESH_HEADER_VIEW) { _refreshLabel.text = self .textRelease; [_refreshArrow layer].transform = CATransform3DMakeRotation(M_PI, 0, 0, 1); } else { _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_VIEW) { [ self startLoading]; } } #pragma mark - #pragma mark Pull to refresh methods - ( void )setUpStrings{ _textPull = [[ NSString alloc]initWithString:@ "拖动获取更多。。。" ]; _textRelease = [[ NSString alloc]initWithString:@ "释放获取更多。。。" ]; _textRelease = [[ NSString alloc]initWithString:@ "加载中。。。" ]; } - ( void )addPullToRefreshHeader{ CGRect mainBounds = [[UIScreen mainScreen] bounds]; _refreshHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0 - REFRESH_HEADER_VIEW, mainBounds.size.width, REFRESH_HEADER_VIEW)]; _refreshHeaderView.backgroundColor = [UIColor clearColor]; _refreshLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, mainBounds.size.width, REFRESH_HEADER_VIEW)]; _refreshLabel.backgroundColor = [UIColor clearColor]; _refreshLabel.font = [UIFont boldSystemFontOfSize:12.f]; _refreshLabel.textAlignment = UITextAlignmentCenter; _refreshArrow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@ "arrow.png" ]]; _refreshArrow.frame = CGRectMake(floorf((REFRESH_HEADER_VIEW - 20) / 2), floorf((REFRESH_HEADER_VIEW - 20) / 2), 27, 44); _refreshSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; _refreshSpinner.frame = CGRectMake(floorf((REFRESH_HEADER_VIEW - 20) / 2), floorf((REFRESH_HEADER_VIEW - 20) / 2), 27, 44); _refreshSpinner.hidesWhenStopped = YES ; [_refreshHeaderView addSubview:_refreshLabel]; [_refreshHeaderView addSubview:_refreshArrow]; [_refreshHeaderView addSubview:_refreshSpinner]; [ self .weiboTableView addSubview:_refreshHeaderView]; } - ( void )startLoading{ _isLoading = YES ; [UIView animateWithDuration:0.3f animations:^{ self .weiboTableView.contentInset = UIEdgeInsetsMake(REFRESH_HEADER_VIEW, 0, 0, 0); _refreshLabel.text = self .textLoading; _refreshArrow.hidden = YES ; [_refreshSpinner startAnimating]; }]; [ self refresh]; } - ( void )stopLoadingComplete { // Reset the header _refreshLabel.text = self .textPull; _refreshArrow.hidden = NO ; [_refreshSpinner stopAnimating]; } - ( void )stopLoading{ _isLoading = NO ; [UIView animateWithDuration:0.3f animations:^{ self .weiboTableView.contentInset = UIEdgeInsetsZero; [_refreshArrow layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1); } completion:^( BOOL finished){ [ self performSelector: @selector (stopLoadingComplete)]; }]; } - ( void )refresh{ [ self performSelector: @selector (stopLoading) withObject: nil afterDelay:5.f]; } @end
|
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122
做人要厚道,转载请注明出处!