Ei kuvausta

FKRefreshTopView.m 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // FKRefreshTopView.m
  3. // FKRefreshControl
  4. //
  5. // Created by jack on 15/12/3.
  6. // Copyright © 2015年 FirstLink. All rights reserved.
  7. //
  8. #define RefreshCircleLineW 1.0f
  9. #define RefreshAnimMargin 30.f
  10. #define RefreshNoStrokeBottom 25.0f // 下拉缓存区
  11. #define RefreshRotationDuration 0.7f
  12. @interface FKAnimView : UIView
  13. @property (nonatomic, assign) CGFloat process;
  14. @end
  15. @implementation FKAnimView
  16. - (void)drawRect:(CGRect)rect{
  17. [super drawRect:rect];
  18. CGFloat areaMargin = RefreshAnimMargin + RefreshCircleLineW;
  19. CGContextRef context = UIGraphicsGetCurrentContext();
  20. CGContextSetStrokeColorWithColor(context, UIColorFromRGB(0x444444).CGColor);
  21. CGContextSetLineWidth(context, RefreshCircleLineW);
  22. CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
  23. CGContextAddArc(context, center.x, center.y, areaMargin / 2.0f - RefreshCircleLineW + 1, - M_PI_2, self.process * 2 * M_PI - M_PI_2, NO);
  24. CGContextDrawPath(context, kCGPathStroke);
  25. }
  26. @end
  27. #import "FKRefreshTopView.h"
  28. #define FKRefreshTopMargin 65.0f
  29. static NSInteger const MaxGifImageCount = 32;
  30. @interface FKRefreshTopView ()
  31. @property (nonatomic, strong) UIView *containerView;
  32. @property (nonatomic, strong) CAShapeLayer *circleLayer;
  33. @property (nonatomic, strong) CAShapeLayer *bgLayer;
  34. @property (nonatomic, strong) UIView *coverView;
  35. @property (nonatomic, strong) FKAnimView *circleView;
  36. @property (nonatomic,strong)UIImageView * imageView;
  37. @property (nonatomic,strong)UIImageView * gifImgView;
  38. @end
  39. @implementation FKRefreshTopView
  40. - (instancetype)initWithFrame:(CGRect)frame{
  41. if (self = [super initWithFrame:frame]) {
  42. [self addSubview:self.imageView];
  43. [self addSubview:self.gifImgView];
  44. }
  45. return self;
  46. }
  47. #pragma mark - FKRefreshDelegate
  48. - (void)resetLayoutSubViews{
  49. // 重新布局
  50. CGFloat height = CGRectGetHeight(self.bounds);
  51. CGFloat width = CGRectGetWidth(self.bounds);
  52. if (!height || !width) return;
  53. self.imageView.bounds = CGRectMake(0, 0, 32, 32);
  54. self.imageView.center = CGPointMake(width / 2.0f, CGRectGetMaxY(self.bounds) - 28);
  55. self.gifImgView.center = self.imageView.center;
  56. self.gifImgView.bounds = self.imageView.bounds;
  57. }
  58. - (void)didDisengageRefreshWithOffsetY:(CGFloat)y{
  59. // CGFloat process = - (y + RefreshNoStrokeBottom) / (FKRefreshTopMargin - RefreshNoStrokeBottom);
  60. // if (process < 0) process = 0;
  61. // self.circleView.process = process;
  62. // [self.circleView setNeedsDisplay];
  63. }
  64. - (void)canEngageRefreshWithOffsetY:(CGFloat)y{
  65. // CGFloat process = - (y + RefreshNoStrokeBottom) / (FKRefreshTopMargin - RefreshNoStrokeBottom);
  66. // if (process < 0) process = 0;
  67. // self.circleView.process = process;
  68. //
  69. // [self.circleView setNeedsDisplay];
  70. }
  71. - (void)startRefreshing{
  72. self.imageView.hidden = TRUE;
  73. self.gifImgView.hidden = FALSE;
  74. self.gifImgView.animationRepeatCount = 0;
  75. [self.gifImgView startAnimating];
  76. }
  77. - (void)finishRefreshing{
  78. self.imageView.hidden = FALSE;
  79. self.gifImgView.hidden = TRUE;
  80. [self.gifImgView stopAnimating];
  81. }
  82. #pragma mark - property
  83. - (UIImageView *)imageView{
  84. if (_imageView == nil) {
  85. _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"imageBg"]];
  86. }
  87. return _imageView;
  88. }
  89. - (UIImageView *)gifImgView {
  90. if (!_gifImgView) {
  91. _gifImgView = [[UIImageView alloc] init];
  92. UIImage *image = nil;
  93. NSMutableArray *array = [NSMutableArray array];
  94. for (int idx = 1 ; idx <= MaxGifImageCount; idx++) {
  95. image = [UIImage imageNamed:[NSString stringWithFormat:@"pullimage%@", @(idx)]];
  96. if (image) {
  97. [array addObject:image];
  98. }
  99. }
  100. _gifImgView.animationImages = array;
  101. _gifImgView.animationDuration = 1.0f;
  102. }
  103. return _gifImgView;
  104. }
  105. - (FKAnimView *)circleView {
  106. if (_circleView == nil) {
  107. _circleView = [[FKAnimView alloc]init];
  108. _circleView.backgroundColor = [UIColor clearColor];
  109. _circleView.contentMode = UIViewContentModeRedraw;
  110. }
  111. return _circleView;
  112. }
  113. @end