RefreshTableHeaderView.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #import "RefreshTableHeaderView.h"
  2. #define TEXT_COLOR [UIColor colorWithRed:20.0/255.0 green:20.0/255.0 blue:20.0/255.0 alpha:1.0]
  3. #define BORDER_COLOR [UIColor colorWithRed:87.0/255.0 green:108.0/255.0 blue:137.0/255.0 alpha:1.0]
  4. #define FLIP_ANIMATION_DURATION 0.18f
  5. @implementation RefreshTableHeaderView
  6. @synthesize state;
  7. - (id)initWithFrame:(CGRect)frame {
  8. if (self = [super initWithFrame:frame]) {
  9. self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  10. mTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 30.0f, self.frame.size.width, 20.0f)];
  11. mTimeLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  12. mTimeLabel.font = [UIFont systemFontOfSize:12.0f];
  13. mTimeLabel.textColor = TEXT_COLOR;
  14. mTimeLabel.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
  15. mTimeLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  16. mTimeLabel.backgroundColor = [UIColor clearColor];
  17. mTimeLabel.textAlignment = NSTextAlignmentCenter;
  18. [self addSubview:mTimeLabel];
  19. // [mTimeLabel release];
  20. if ([[NSUserDefaults standardUserDefaults] objectForKey:@"EGORefreshTableView_LastRefresh"]) {
  21. mTimeLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"EGORefreshTableView_LastRefresh"];
  22. }
  23. else {
  24. [self setCurrentDate];
  25. }
  26. mStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 48.0f, self.frame.size.width, 20.0f)];
  27. mStatusLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  28. mStatusLabel.font = [UIFont boldSystemFontOfSize:13.0f];
  29. mStatusLabel.textColor = TEXT_COLOR;
  30. mStatusLabel.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
  31. mStatusLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  32. mStatusLabel.backgroundColor = [UIColor clearColor];
  33. mStatusLabel.textAlignment = NSTextAlignmentCenter;
  34. [self addSubview:mStatusLabel];
  35. //[mStatusLabel release];
  36. mStatusImage = [[CALayer alloc] init];
  37. mStatusImage.frame = CGRectMake(25.0f, frame.size.height - 65.0f, 30.0f, 55.0f);
  38. mStatusImage.contentsGravity = kCAGravityResizeAspect;
  39. mStatusImage.contents = (id)[UIImage imageNamed:@"blackArrow.png"].CGImage;
  40. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
  41. if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
  42. mStatusImage.contentsScale = [[UIScreen mainScreen] scale];
  43. }
  44. #endif
  45. [[self layer] addSublayer:mStatusImage];
  46. //[mStatusImage release];
  47. mActView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  48. mActView.frame = CGRectMake(25.0f, frame.size.height - 38.0f, 20.0f, 20.0f);
  49. [self addSubview:mActView];
  50. // [mActView release];
  51. UIImage *backImage = [UIImage imageNamed:@"headback.png"];
  52. if (backImage) {
  53. self.backgroundColor = [UIColor colorWithPatternImage:backImage];
  54. }
  55. else {
  56. // self.backgroundColor = [UIColor colorWithRed:150.0/255.0 green:150.0/255.0 blue:150.0/255.0 alpha:1.0];
  57. self.backgroundColor = [UIColor whiteColor];
  58. }
  59. [self setState:PullRefreshNormal];
  60. }
  61. return self;
  62. }
  63. - (void)setCurrentDate {
  64. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  65. // [formatter setAMSymbol:@"上午"];
  66. // [formatter setPMSymbol:@"下午"];
  67. [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
  68. mTimeLabel.text = [NSString stringWithFormat:@"上次更新时间: %@", [formatter stringFromDate:[NSDate date]]];
  69. [[NSUserDefaults standardUserDefaults] setObject:mTimeLabel.text forKey:@"EGORefreshTableView_LastRefresh"];
  70. [[NSUserDefaults standardUserDefaults] synchronize];
  71. // [formatter release];
  72. }
  73. /*
  74. - (void)drawRect:(CGRect)rect{
  75. UIImage *image=[UIImage imageNamed:@"background.png"];
  76. [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
  77. }
  78. */
  79. - (void)setState:(PullRefreshState)aState{
  80. switch (aState) {
  81. case PullRefreshPulling:
  82. mStatusLabel.text = @"松开更新...";
  83. [CATransaction begin];
  84. [CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];
  85. mStatusImage.transform = CATransform3DMakeRotation((M_PI / 180.0) * 180.0f, 0.0f, 0.0f, 1.0f);
  86. [CATransaction commit];
  87. break;
  88. case PullRefreshNormal:
  89. if (state == PullRefreshPulling) {
  90. [CATransaction begin];
  91. [CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];
  92. mStatusImage.transform = CATransform3DIdentity;
  93. [CATransaction commit];
  94. }
  95. mStatusLabel.text = @"下拉更新...";
  96. [mActView stopAnimating];
  97. [CATransaction begin];
  98. [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
  99. mStatusImage.hidden = NO;
  100. mStatusImage.transform = CATransform3DIdentity;
  101. [CATransaction commit];
  102. break;
  103. case PullRefreshLoading:
  104. mStatusLabel.text = @"加载中...";
  105. [mActView startAnimating];
  106. [CATransaction begin];
  107. [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
  108. mStatusImage.hidden = YES;
  109. [CATransaction commit];
  110. break;
  111. default:
  112. break;
  113. }
  114. state = aState;
  115. }
  116. - (void)dealloc {
  117. // [super dealloc];
  118. }
  119. @end