Нет описания

FKRefreshBottomView.m 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // RefreshBottomView.m
  3. //
  4. // Copyright (c) 2014 YDJ ( https://github.com/ydj/RefreshControl )
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #import "FKRefreshBottomView.h"
  24. @implementation FKRefreshBottomView
  25. - (void)layoutSubviews
  26. {
  27. [super layoutSubviews];
  28. }
  29. - (instancetype)initWithFrame:(CGRect)frame
  30. {
  31. self=[super initWithFrame:frame];
  32. if (self)
  33. {
  34. [self initViews];
  35. }
  36. return self;
  37. }
  38. - (void)initViews
  39. {
  40. // self.backgroundColor=UIColorFromRGB(0xeeeeee);
  41. self.backgroundColor = [UIColor whiteColor];
  42. _activityIndicatorView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  43. _activityIndicatorView.hidesWhenStopped=YES;
  44. // _activityIndicatorView.color=[UIColor orangeColor];
  45. _activityIndicatorView.color = UIColorFromRGB(0x333333);
  46. _activityIndicatorView.translatesAutoresizingMaskIntoConstraints=NO;
  47. [self addSubview:_activityIndicatorView];
  48. _loadingLabel=[[UILabel alloc] initWithFrame:CGRectZero];
  49. _loadingLabel.backgroundColor=[UIColor clearColor];
  50. _loadingLabel.font=[UIFont systemFontOfSize:13];
  51. _loadingLabel.translatesAutoresizingMaskIntoConstraints=NO;
  52. [self addSubview:_loadingLabel];
  53. _promptLabel=[[UILabel alloc] initWithFrame:CGRectZero];
  54. _promptLabel.backgroundColor=[UIColor clearColor];
  55. _promptLabel.font=[UIFont systemFontOfSize:13];
  56. _promptLabel.textAlignment=NSTextAlignmentCenter;
  57. _promptLabel.translatesAutoresizingMaskIntoConstraints=NO;
  58. [self addSubview:_promptLabel];
  59. [self resetViews];
  60. [self resetLayoutSubViews];
  61. }
  62. - (void)resetViews
  63. {
  64. _promptLabel.hidden=NO;
  65. _promptLabel.text=@"上拉加载更多";
  66. _loadingLabel.hidden=YES;
  67. _loadingLabel.text=@"正在加载...";
  68. if ([self.activityIndicatorView isAnimating])
  69. {
  70. [self.activityIndicatorView stopAnimating];
  71. }
  72. }
  73. - (void)resetLayoutSubViews
  74. {
  75. NSArray * tempContraint=self.constraints;
  76. if ([tempContraint count]>0)
  77. {
  78. [self removeConstraints:tempContraint];
  79. }
  80. [UIView animateWithDuration:0.25 animations:^{
  81. /////
  82. NSLayoutConstraint * aTop=[NSLayoutConstraint constraintWithItem:self.activityIndicatorView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:13];
  83. NSLayoutConstraint * aRight=[NSLayoutConstraint constraintWithItem:self.activityIndicatorView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:-10];
  84. NSLayoutConstraint * aWith=[NSLayoutConstraint constraintWithItem:self.activityIndicatorView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:0 constant:35];
  85. NSLayoutConstraint * aHeight=[NSLayoutConstraint constraintWithItem:self.activityIndicatorView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:0 constant:35];
  86. NSArray * aList=@[aTop,aRight,aWith,aHeight];
  87. [self addConstraints:aList];
  88. /////////////
  89. NSLayoutConstraint * tLeft=[NSLayoutConstraint constraintWithItem:self.loadingLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:-12];
  90. NSLayoutConstraint * tTop=[NSLayoutConstraint constraintWithItem:self.loadingLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:13];
  91. NSLayoutConstraint * tRight=[NSLayoutConstraint constraintWithItem:self.loadingLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0];
  92. NSLayoutConstraint * tHeight=[NSLayoutConstraint constraintWithItem:self.loadingLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:0 constant:32];
  93. NSArray * tList=@[tLeft,tTop,tRight,tHeight];
  94. [self addConstraints:tList];
  95. ///////
  96. NSDictionary * viewsDictionary=@{@"promptLabel":self.promptLabel};
  97. NSArray *pHList=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[promptLabel]-0-|" options:0 metrics:nil views:viewsDictionary];
  98. NSArray *pVList=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[promptLabel(==45)]" options:0 metrics:nil views:viewsDictionary];
  99. [self addConstraints:pHList];
  100. [self addConstraints:pVList];
  101. }];
  102. }
  103. ///松开可刷新
  104. - (void)canEngageRefreshWithOffsetY:(CGFloat)y
  105. {
  106. _promptLabel.text=@"松开即可加载";
  107. }
  108. ///松开返回
  109. - (void)didDisengageRefreshWithOffsetY:(CGFloat)y
  110. {
  111. [self resetViews];
  112. }
  113. ///开始刷新
  114. - (void)startRefreshing
  115. {
  116. _promptLabel.hidden=YES;
  117. _loadingLabel.hidden=NO;
  118. [self.activityIndicatorView startAnimating];
  119. }
  120. ///结束
  121. - (void)finishRefreshing
  122. {
  123. [self resetViews];
  124. }
  125. @end