dkahgld

FMPlaceholderView.m 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // FMPlaceholderView.m
  3. // FMListPlaceholderExample
  4. //
  5. // Created by Mingo on 2018/8/9.
  6. // Copyright © 2017年 袁凤鸣. All rights reserved.
  7. // 项目地址:https://github.com/yfming93/FMListPlaceholder
  8. // 作者博客:https://www.yfmingo.cn
  9. //
  10. #import "FMPlaceholderView.h"
  11. #import "FMListPlaceholder.h"
  12. #import "NSBundle+FMListPlaceholder.h"
  13. #define kFMScreenW [UIScreen mainScreen].bounds.size.width //屏幕宽度
  14. #define kFMScreenH [UIScreen mainScreen].bounds.size.height //屏幕高度
  15. #define kFit6(x) ((kFMScreenW)*((x)/375.0))
  16. #define kTipsH kFit6(22)
  17. @interface FMPlaceholderView ()
  18. @property (nonatomic, strong) NSString *defaultEmptyTips;
  19. @property (nonatomic, strong) NSString *defaultEmptyCoverName;
  20. @end
  21. @implementation FMPlaceholderView
  22. - (instancetype)initWithFrame:(CGRect)frame{
  23. if (self = [super initWithFrame:frame]) {
  24. self.userInteractionEnabled = YES;
  25. _defaultEmptyTips = @"还没有记录";
  26. _coverSpaceToTips = kFit6(10);
  27. _coverCenterYOffset = 0;
  28. self.hidden = YES;
  29. _defaultEmptyCoverName=@"no_order";
  30. // if (FMListPlaceholder.sharedInstance.defaultBackgroundColor) {
  31. // self.backgroundColor = FMListPlaceholder.sharedInstance.defaultBackgroundColor;
  32. // }
  33. //
  34. // if (FMListPlaceholder.sharedInstance.defaultTips.length) {
  35. // _defaultEmptyTips = FMListPlaceholder.sharedInstance.defaultTips;
  36. // }
  37. //
  38. // if (FMListPlaceholder.sharedInstance.defaultCoverName.length) {
  39. // _defaultEmptyCoverName = FMListPlaceholder.sharedInstance.defaultCoverName;
  40. // }
  41. [self createUI];
  42. }
  43. return self;
  44. }
  45. -(void)setCoverSize:(CGSize)coverSize {
  46. _coverSize = coverSize;
  47. _emptyCover.frame = CGRectMake((self.frame.size.width - coverSize.width)/2,
  48. (self.frame.size.height - coverSize.height) - _coverSpaceToTips - kTipsH,
  49. coverSize.width,
  50. coverSize.height);
  51. _emptyCover.center = CGPointMake(self.center.x, self.center.y - _coverSpaceToTips/2 - kTipsH/2 + _coverCenterYOffset);
  52. _emptyTips.frame = CGRectMake(0,
  53. CGRectGetMaxY(self.emptyCover.frame) + _coverSpaceToTips,
  54. self.frame.size.width,
  55. kTipsH);
  56. }
  57. -(void)setCoverCenterYOffset:(CGFloat)coverCenterYOffset {
  58. _coverCenterYOffset = coverCenterYOffset;
  59. _emptyCover.center = CGPointMake(self.center.x, self.center.y - _coverSpaceToTips/2 - kTipsH/2 + coverCenterYOffset);
  60. _emptyTips.frame = CGRectMake(0,
  61. CGRectGetMaxY(self.emptyCover.frame) + _coverSpaceToTips,
  62. self.frame.size.width,
  63. kTipsH);
  64. }
  65. -(void)setcoverSpaceToTips:(CGFloat)coverSpaceToTips {
  66. _coverSpaceToTips = coverSpaceToTips;
  67. _emptyCover.center = CGPointMake(self.center.x, self.center.y - _coverSpaceToTips/2 - kTipsH/2 );
  68. _emptyTips.frame = CGRectMake(0,
  69. CGRectGetMaxY(self.emptyCover.frame) + _coverSpaceToTips,
  70. self.frame.size.width,
  71. kTipsH);
  72. }
  73. -(void)setDefaultPlaceholder:(UIView *)defaultPlaceholder {
  74. _defaultPlaceholder = defaultPlaceholder;
  75. for (UIView *v in self.subviews) {
  76. [v removeFromSuperview];
  77. }
  78. [self addSubview:_defaultPlaceholder];
  79. _defaultPlaceholder.center = self.center;
  80. _defaultPlaceholder.userInteractionEnabled = YES;
  81. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
  82. [_defaultPlaceholder addGestureRecognizer:tap];
  83. if (self.coverCenterYOffset) {
  84. _defaultPlaceholder.center = CGPointMake(self.center.x, self.center.y - _coverSpaceToTips/2);
  85. }
  86. if (self.coverSize.width) {
  87. [_defaultPlaceholder setBounds:CGRectMake(0, 0, self.coverSize.width, self.coverSize.height)];
  88. }
  89. }
  90. - (UIImageView *)emptyCover{
  91. if (!_emptyCover) {
  92. _emptyCover =[[UIImageView alloc] initWithImage:[UIImage imageNamed:_defaultEmptyCoverName]];
  93. if (!_defaultEmptyCoverName.length) {
  94. UIImage *ima = [NSBundle fm_coverImage];
  95. [_emptyCover setImage:ima];
  96. }
  97. _emptyCover.contentMode = UIViewContentModeScaleToFill;
  98. CGSize coverSize = CGSizeMake(self.frame.size.width/5, self.frame.size.width/5);
  99. if (FMListPlaceholder.sharedInstance.defaultCoverSize.width) {
  100. coverSize = FMListPlaceholder.sharedInstance.defaultCoverSize;
  101. }
  102. _emptyCover.frame = CGRectMake((self.frame.size.width - coverSize.width)/2,
  103. (self.frame.size.height - coverSize.height) - _coverSpaceToTips - kTipsH,
  104. coverSize.width,
  105. coverSize.height);
  106. CGFloat OffsetY = FMListPlaceholder.sharedInstance.coverCenterYOffset;
  107. if (self.coverCenterYOffset) {
  108. OffsetY = self.coverCenterYOffset;
  109. }
  110. _emptyCover.center = CGPointMake(self.center.x, self.center.y - _coverSpaceToTips/2 - kTipsH/2 + OffsetY );
  111. _emptyCover.userInteractionEnabled = YES;
  112. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
  113. [_emptyCover addGestureRecognizer:tap];
  114. }
  115. return _emptyCover;
  116. }
  117. - (UIButton *)emptyTips{
  118. if (!_emptyTips) {
  119. _emptyTips =[[UIButton alloc] init];
  120. [_emptyTips setTitle:_defaultEmptyTips forState:UIControlStateNormal];
  121. _emptyTips.titleLabel.textAlignment = NSTextAlignmentCenter;
  122. _emptyTips.titleLabel.font = [UIFont systemFontOfSize:kFit6(16)];
  123. CGFloat coverSpaceToTips = FMListPlaceholder.sharedInstance.coverSpaceToTips;
  124. if (coverSpaceToTips) {
  125. _coverSpaceToTips = coverSpaceToTips;
  126. }
  127. _emptyTips.frame = CGRectMake(0,
  128. CGRectGetMaxY(self.emptyCover.frame) + _coverSpaceToTips,
  129. self.frame.size.width,
  130. kTipsH);
  131. _emptyTips.center = CGPointMake(self.center.x, _emptyTips.center.y);
  132. [_emptyTips addTarget:self action:@selector(tapAction) forControlEvents:UIControlEventTouchUpInside];
  133. if (FMListPlaceholder.sharedInstance.defaultTipsTextColor) {
  134. [_emptyTips setTitleColor:FMListPlaceholder.sharedInstance.defaultTipsTextColor forState:UIControlStateNormal];
  135. }else{
  136. [_emptyTips setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
  137. }
  138. if (FMListPlaceholder.sharedInstance.defaultTipsFont) {
  139. [_emptyTips.titleLabel setFont:FMListPlaceholder.sharedInstance.defaultTipsFont];
  140. }
  141. }
  142. return _emptyTips;
  143. }
  144. - (void)tapAction {
  145. if (self.reloadClickBlock) {
  146. self.reloadClickBlock();
  147. [UIView animateWithDuration:1.0 animations:^{
  148. self.emptyCover.alpha = 0.5;
  149. self.emptyTips.alpha = 0.5;
  150. FMListPlaceholder.sharedInstance.defaultPlaceholder.alpha = 0.5;
  151. [UIView animateWithDuration:0.5 animations:^{
  152. self.emptyCover.alpha = 1.0;
  153. self.emptyTips.alpha = 1.0;
  154. FMListPlaceholder.sharedInstance.defaultPlaceholder.alpha = 1.0;
  155. }];
  156. }];
  157. }
  158. }
  159. - (void)createUI {
  160. if (FMListPlaceholder.sharedInstance.defaultPlaceholder != nil) {
  161. [self addSubview:FMListPlaceholder.sharedInstance.defaultPlaceholder];
  162. FMListPlaceholder.sharedInstance.defaultPlaceholder.center = self.center;
  163. FMListPlaceholder.sharedInstance.defaultPlaceholder.userInteractionEnabled = YES;
  164. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
  165. [FMListPlaceholder.sharedInstance.defaultPlaceholder addGestureRecognizer:tap];
  166. } else {
  167. [self addSubview:self.emptyCover];
  168. [self addSubview:self.emptyTips];
  169. }
  170. }
  171. @end