EGORefreshTableHeaderViews.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // EGORefreshTableHeaderView.m
  3. // Demo
  4. //
  5. // Created by Devin Doty on 10/14/09October14.
  6. // Copyright 2009 enormego. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #import "EGORefreshTableHeaderViews.h"
  27. #define TEXT_COLOR [UIColor colorWithRed:87.0/255.0 green:108.0/255.0 blue:137.0/255.0 alpha:1.0]
  28. #define FLIP_ANIMATION_DURATION 0.18f
  29. @interface EGORefreshTableHeaderViews (Private)
  30. - (void)setState:(EGOPullRefreshState)aState;
  31. @end
  32. @implementation EGORefreshTableHeaderViews
  33. @synthesize delegate=_delegate;
  34. @synthesize isMessageVC = _isMessageVC;
  35. - (id)initWithFrame:(CGRect)frame {
  36. if (self = [super initWithFrame:frame]) {
  37. _isMessageVC = NO;
  38. self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  39. self.backgroundColor = [UIColor colorWithRed:226.0/255.0 green:231.0/255.0 blue:237.0/255.0 alpha:1.0];
  40. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 30.0f, self.frame.size.width, 20.0f)];
  41. label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  42. label.font = [UIFont systemFontOfSize:12.0f];
  43. label.textColor = TEXT_COLOR;
  44. //label.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
  45. //label.shadowOffset = CGSizeMake(0.0f, 1.0f);
  46. label.backgroundColor = [UIColor clearColor];
  47. label.textAlignment = NSTextAlignmentCenter;// UITextAlignmentCenter;
  48. [self addSubview:label];
  49. _lastUpdatedLabel=label;
  50. //[label release];
  51. label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 48.0f, self.frame.size.width, 20.0f)];
  52. label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  53. label.font = [UIFont boldSystemFontOfSize:13.0f];
  54. label.textColor = TEXT_COLOR;
  55. //label.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
  56. //label.shadowOffset = CGSizeMake(0.0f, 1.0f);
  57. label.backgroundColor = [UIColor clearColor];
  58. label.textAlignment = NSTextAlignmentCenter;
  59. [self addSubview:label];
  60. _statusLabel=label;
  61. //[label release];
  62. CALayer *layer = [CALayer layer];
  63. layer.frame = CGRectMake(25.0f, frame.size.height - 65.0f, 30.0f, 55.0f);
  64. layer.contentsGravity = kCAGravityResizeAspect;
  65. layer.contents = (id)[UIImage imageNamed:@"tableview_pulltorefresh_arrow.png"].CGImage;
  66. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
  67. if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
  68. layer.contentsScale = [[UIScreen mainScreen] scale];
  69. }
  70. #endif
  71. [[self layer] addSublayer:layer];
  72. _arrowImage=layer;
  73. UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  74. view.frame = CGRectMake(25.0f, frame.size.height - 38.0f, 20.0f, 20.0f);
  75. [self addSubview:view];
  76. _activityView = view;
  77. //[view release];
  78. /*_activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  79. _activityView.frame = CGRectMake(25.0f, frame.size.height - 38.0f, 20.0f, 20.0f);*/
  80. [self addSubview:_activityView];
  81. [self setState:EGOOPullRefreshNormal];
  82. }
  83. return self;
  84. }
  85. #pragma mark -
  86. #pragma mark Setters
  87. - (void)refreshLastUpdatedDate {
  88. if ([_delegate respondsToSelector:@selector(egoRefreshTableHeaderDataSourceLastUpdated:)]) {
  89. // if(1 == 1){
  90. NSDate *date = [_delegate egoRefreshTableHeaderDataSourceLastUpdated:self];
  91. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  92. [formatter setAMSymbol:@"AM"];
  93. [formatter setPMSymbol:@"PM"];
  94. [formatter setDateFormat:@"MM/dd/yyyy HH:mm"];
  95. _lastUpdatedLabel.text = [NSString stringWithFormat:@"上次刷新: %@", [formatter stringFromDate:date]];
  96. [[NSUserDefaults standardUserDefaults] setObject:_lastUpdatedLabel.text forKey:@"EGORefreshTableView_LastRefresh"];
  97. [[NSUserDefaults standardUserDefaults] synchronize];
  98. //[formatter release];
  99. } else {
  100. _lastUpdatedLabel.text = nil;
  101. }
  102. }
  103. - (void)setState:(EGOPullRefreshState)aState{
  104. switch (aState) {
  105. case EGOOPullRefreshPulling:
  106. if (_isMessageVC) {
  107. _statusLabel.text = NSLocalizedString(@"松开即可加载...", @"Release to refresh status");
  108. }else{
  109. _statusLabel.text = NSLocalizedString(@"松开即可刷新...", @"Release to refresh status");
  110. }
  111. [CATransaction begin];
  112. [CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];
  113. _arrowImage.transform = CATransform3DMakeRotation((M_PI / 180.0) * 180.0f, 0.0f, 0.0f, 1.0f);
  114. [CATransaction commit];
  115. break;
  116. case EGOOPullRefreshNormal:
  117. if (_state == EGOOPullRefreshPulling) {
  118. [CATransaction begin];
  119. [CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];
  120. _arrowImage.transform = CATransform3DIdentity;
  121. [CATransaction commit];
  122. }
  123. if (_isMessageVC) {
  124. _statusLabel.text = NSLocalizedString(@"下拉获取历史...", @"Pull down to refresh status");
  125. }else{
  126. _statusLabel.text = NSLocalizedString(@"下拉可以刷新...", @"Pull down to refresh status");
  127. }
  128. //_activityView.backgroundColor = [UIColor redColor];
  129. [_activityView stopAnimating];
  130. [CATransaction begin];
  131. [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
  132. _arrowImage.hidden = NO;
  133. _arrowImage.transform = CATransform3DIdentity;
  134. [CATransaction commit];
  135. [self refreshLastUpdatedDate];
  136. break;
  137. case EGOOPullRefreshLoading:
  138. _statusLabel.text = NSLocalizedString(@"载入中...", @"Loading Status");
  139. [_activityView startAnimating];
  140. [CATransaction begin];
  141. [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
  142. _arrowImage.hidden = YES;
  143. [CATransaction commit];
  144. break;
  145. default:
  146. break;
  147. }
  148. _state = aState;
  149. }
  150. #pragma mark -
  151. #pragma mark ScrollView Methods
  152. - (void)egoRefreshScrollViewDidScroll:(UIScrollView *)scrollView {
  153. if (_state == EGOOPullRefreshLoading) {
  154. CGFloat offset = MAX(scrollView.contentOffset.y * -1, 0);
  155. offset = MIN(offset, 60);
  156. scrollView.contentInset = UIEdgeInsetsMake(offset, 0.0f, 0.0f, 0.0f);
  157. } else if (scrollView.isDragging) {
  158. BOOL _loading = NO;
  159. if ([_delegate respondsToSelector:@selector(egoRefreshTableHeaderDataSourceIsLoading:)]) {
  160. // if(1 == 1){
  161. _loading = [_delegate egoRefreshTableHeaderDataSourceIsLoading:self];
  162. }
  163. if (_state == EGOOPullRefreshPulling && scrollView.contentOffset.y > -65.0f && scrollView.contentOffset.y < 0.0f && !_loading) {
  164. [self setState:EGOOPullRefreshNormal];
  165. } else if (_state == EGOOPullRefreshNormal && scrollView.contentOffset.y < -65.0f && !_loading) {
  166. [self setState:EGOOPullRefreshPulling];
  167. }
  168. if (scrollView.contentInset.top != 0) {
  169. scrollView.contentInset = UIEdgeInsetsZero;
  170. }
  171. }
  172. }
  173. - (void)egoRefreshScrollViewDidEndDragging:(UIScrollView *)scrollView {
  174. BOOL _loading = NO;
  175. if ([_delegate respondsToSelector:@selector(egoRefreshTableHeaderDataSourceIsLoading:)]) {
  176. // if(1 == 1){
  177. _loading = [_delegate egoRefreshTableHeaderDataSourceIsLoading:self];
  178. }
  179. if (scrollView.contentOffset.y <= - 65.0f && !_loading) {
  180. if ([_delegate respondsToSelector:@selector(egoRefreshTableHeaderDidTriggerRefresh:)]) {
  181. // if(1 == 1){
  182. [_delegate egoRefreshTableHeaderDidTriggerRefresh:self];
  183. }
  184. [self setState:EGOOPullRefreshLoading];
  185. [UIView beginAnimations:nil context:NULL];
  186. [UIView setAnimationDuration:0.2];
  187. scrollView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);
  188. [UIView commitAnimations];
  189. }
  190. }
  191. - (void)egoRefreshScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView {
  192. [UIView beginAnimations:nil context:NULL];
  193. [UIView setAnimationDuration:.3];
  194. [scrollView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
  195. [UIView commitAnimations];
  196. [self setState:EGOOPullRefreshNormal];
  197. }
  198. //扩展:下拉显示加载
  199. - (void)initLoading:(UIScrollView *)scrollView {
  200. // [UIView beginAnimations:nil context:nil];
  201. // [UIView setAnimationDuration:0.3];
  202. scrollView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);
  203. scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, -60);
  204. [self setState:EGOOPullRefreshLoading];
  205. // [UIView commitAnimations];
  206. }
  207. #pragma mark -
  208. #pragma mark Dealloc
  209. /*- (void)dealloc {
  210. _delegate = nil;
  211. _activityView = nil;
  212. _statusLabel = nil;
  213. _arrowImage = nil;
  214. _lastUpdatedLabel = nil;
  215. [super dealloc];
  216. }
  217. */
  218. @end