JX_DownListView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // JX_DownListView.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 17/5/24.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "JX_DownListView.h"
  9. @interface JX_DownListView ()
  10. @property (nonnull, strong) UIView *backgroundView;
  11. @property (nonatomic, strong) DownListPopOptionBlock optionBlock;
  12. @property (nonatomic, strong) UIImageView *backImgV;
  13. @end
  14. @implementation JX_DownListView
  15. -(instancetype)initWithFrame:(CGRect)frame {
  16. if (self = [super initWithFrame:frame]) {
  17. self.frame = frame;
  18. self.alpha = 0.0f;
  19. self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
  20. }
  21. return self;
  22. }
  23. -(instancetype)downlistPopOption:(DownListPopOptionBlock)block whichFrame:(CGRect)frame animate:(BOOL)animate {
  24. self.optionBlock = block;
  25. [self setupParams:animate];
  26. if (self.maxWidth > 0) {
  27. if (self.maxWidth + 50 < self.frame.size.width*self.mutiple) {
  28. self.maxWidth = self.frame.size.width*self.mutiple - 30;
  29. }else {
  30. self.maxWidth = self.maxWidth + 20;
  31. }
  32. }else {
  33. for (NSInteger i = 0; i < self.listContents.count; i ++) {
  34. NSString *str = self.listContents[i];
  35. NSString *imageStr = self.listImages[i];
  36. CGSize size = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0]} context:nil].size;
  37. if (imageStr.length > 0) {
  38. if (size.width > self.maxWidth - 60) {
  39. self.maxWidth = size.width + 35 + (self.lineHeight-23);
  40. }
  41. }else {
  42. if (size.width > self.maxWidth) {
  43. self.maxWidth = size.width + 20;
  44. }
  45. }
  46. }
  47. }
  48. [self setupBackgourndview:frame];
  49. return self;
  50. }
  51. -(void)show {
  52. [UIView animateWithDuration:self.animateTime animations:^{
  53. self.alpha = 1.0f;
  54. } completion:^(BOOL finished) {
  55. // UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesturePressed)];
  56. // [self addGestureRecognizer:tapGesture];
  57. }];
  58. }
  59. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  60. [self tapGesturePressed];
  61. }
  62. #pragma mark - private
  63. -(void) setupParams:(BOOL)animate {
  64. if (self.lineHeight == 0) {
  65. self.lineHeight = 40.0f;
  66. }
  67. if (self.mutiple == 0) {
  68. self.mutiple = 0.4f;
  69. }
  70. if (animate) {
  71. if (self.animateTime == 0) {
  72. self.animateTime = 0.2f;
  73. }
  74. } else {
  75. self.animateTime = 0;
  76. }
  77. }
  78. // 创建背景
  79. - (void) setupBackgourndview:(CGRect)whichFrame {
  80. self.backgroundView = [UIView new];
  81. self.backgroundView.backgroundColor = [UIColor clearColor];
  82. self.backgroundView.layer.cornerRadius = 5;
  83. self.backgroundView.layer.masksToBounds = YES;
  84. // self.backgroundView.layer.borderWidth = 0.5;
  85. // self.backgroundView.layer.borderColor = [HEXCOLOR(0xd8d8d8) CGColor];
  86. [self addSubview:self.backgroundView];
  87. //背景图片
  88. self.backImgV = [[UIImageView alloc]init];
  89. UIImage *image = [[UIImage imageNamed:@"Haircircleoffriends_white"] resizableImageWithCapInsets:UIEdgeInsetsMake(25, 10, 25, 10) resizingMode:UIImageResizingModeStretch];
  90. self.backImgV.image = image;
  91. [self.backgroundView addSubview:self.backImgV];
  92. [self tochangeBackgroudViewFrame:whichFrame];
  93. [self setupOptionButton];
  94. }
  95. - (void) setupOptionButton {
  96. if ((self.listContents&&self.listContents.count>0)) {
  97. for (NSInteger i = 0; i < self.listContents.count; i++) {
  98. UIButton *optionButton = [UIButton buttonWithType:UIButtonTypeCustom];
  99. int addY = 10;
  100. if (self.showType == DownListView_ShowUp || self.showType == DownListView_Center) {
  101. addY = 0;
  102. }
  103. optionButton.frame = CGRectMake(0,
  104. self.lineHeight*i + addY,
  105. self.maxWidth,
  106. self.lineHeight);
  107. if (_color) {
  108. [optionButton setBackgroundColor:[UIColor clearColor]];
  109. }
  110. optionButton.tag = i;
  111. BOOL unEnable = NO;
  112. if (self.listEnables && self.listEnables.count > 0) {
  113. BOOL enable = [self.listEnables[optionButton.tag] boolValue];
  114. // optionButton.enabled = enable;
  115. if (!enable){
  116. unEnable = YES;
  117. [optionButton addTarget:self action:@selector(noPermission:) forControlEvents:UIControlEventTouchUpInside];
  118. }else{
  119. [optionButton addTarget:self action:@selector(buttonSelectPressed:)
  120. forControlEvents:UIControlEventTouchUpInside];
  121. }
  122. }else{
  123. [optionButton addTarget:self action:@selector(buttonSelectPressed:)
  124. forControlEvents:UIControlEventTouchUpInside];
  125. }
  126. [optionButton addTarget:self action:@selector(buttonSelectDown:)
  127. forControlEvents:UIControlEventTouchDown];
  128. [optionButton addTarget:self action:@selector(buttonSelectOutside:)
  129. forControlEvents:UIControlEventTouchUpOutside];
  130. [self.backgroundView addSubview:optionButton];
  131. [self setupOptionContent:optionButton enable:unEnable];
  132. }
  133. }
  134. }
  135. - (void) setupOptionContent:(UIButton *)optionButton enable:(BOOL)enable{
  136. if(self.listImages && self.listImages.count>0) {
  137. UIImageView *headImageView = [UIImageView new];
  138. headImageView.frame = CGRectMake(12, 11.5, self.lineHeight-23, self.lineHeight-23);
  139. headImageView.image = [UIImage imageNamed:self.listImages[optionButton.tag]];
  140. [optionButton addSubview:headImageView];
  141. UILabel *contentLabel = [UILabel new];
  142. contentLabel.frame = CGRectMake(CGRectGetMaxX(headImageView.frame)+11,
  143. 0,
  144. self.frame.size.width-(CGRectGetMaxX(headImageView.frame)+11),
  145. self.lineHeight);
  146. contentLabel.text = self.listContents[optionButton.tag];
  147. if (self.textColor) {
  148. contentLabel.textColor = self.textColor;
  149. }else {
  150. contentLabel.textColor = [UIColor blackColor];
  151. }
  152. contentLabel.font = [UIFont systemFontOfSize:15];
  153. if (enable) {
  154. contentLabel.textColor = [UIColor grayColor];
  155. headImageView.image = [[UIImage imageNamed:self.listImages[optionButton.tag]] imageWithTintColor:[UIColor grayColor]];
  156. }
  157. [optionButton addSubview:contentLabel];
  158. } else {
  159. UILabel *contentLabel = [UILabel new];
  160. [optionButton addSubview:contentLabel];
  161. contentLabel.frame = optionButton.bounds;
  162. contentLabel.textAlignment = NSTextAlignmentCenter;
  163. contentLabel.text = self.listContents[optionButton.tag];
  164. if (self.textColor) {
  165. contentLabel.textColor = self.textColor;
  166. }else {
  167. contentLabel.textColor = [UIColor blackColor];
  168. }
  169. contentLabel.font = [UIFont systemFontOfSize:15];
  170. if (enable) {
  171. contentLabel.textColor = [UIColor grayColor];
  172. }
  173. }
  174. if(optionButton.tag != 0) {
  175. UIView *lineView = [UIView new];
  176. lineView.backgroundColor = [HEXCOLOR(0xd8d8d8) colorWithAlphaComponent:0.1];
  177. lineView.frame = CGRectMake(0,
  178. 0,
  179. self.maxWidth,
  180. .5);
  181. [optionButton addSubview:lineView];
  182. }
  183. }
  184. - (void) tochangeBackgroudViewFrame:(CGRect)whichFrame {
  185. CGFloat self_w = self.frame.size.width;
  186. CGFloat which_x = whichFrame.origin.x;
  187. CGFloat which_w = whichFrame.size.width;
  188. CGFloat which_h = whichFrame.size.height;
  189. CGFloat background_x = which_x-((self.maxWidth/2)-which_w/2);
  190. CGFloat background_w = self.maxWidth;
  191. CGFloat background_h = self.lineHeight*self.listContents.count+10;
  192. CGFloat background_y;
  193. if (self.showType == DownListView_ShowUp || self.showType == DownListView_Center) {
  194. background_y = whichFrame.origin.y - background_h;
  195. }else {
  196. background_y = whichFrame.origin.y+which_h;
  197. if ((background_y + background_h) > JX_SCREEN_HEIGHT) {
  198. background_y = whichFrame.origin.y - background_h;
  199. self.showType = DownListView_ShowUp;
  200. }
  201. }
  202. if (background_x < 10) {
  203. background_x = 10;
  204. }
  205. // if (self_w-(which_x+which_w)<=10||
  206. // ((self_w*self.mutiple/2)-which_w/2>=(self_w-(which_x+which_w)))) {
  207. // background_x = self_w-(self_w*self.mutiple)-10;
  208. // }
  209. if ((background_x + background_w + 10) > self_w) {
  210. background_x = self_w-(self.maxWidth)-10;
  211. }
  212. self.backgroundView.frame = CGRectMake(background_x, background_y, background_w, background_h);
  213. self.backImgV.frame = self.backgroundView.bounds;
  214. // UIImageView *imageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"downlistSelect_Black"] imageWithTintColor:self.color]];
  215. // [self addSubview:imageView];
  216. // imageView.frame = CGRectMake(which_x+which_w/2-10,
  217. // background_y-15,
  218. // 20,
  219. // 15);
  220. if (self.showType == DownListView_ShowUp) {
  221. self.backImgV.image = [UIImage imageNamed:@"Haircircleoffriends_up"];
  222. // imageView.image = [UIImage imageNamed:@"ic_public_menu"];
  223. // imageView.frame = CGRectMake(imageView.frame.origin.x, background_y + background_h - 6, imageView.frame.size.width, imageView.frame.size.height);
  224. }else if (self.showType == DownListView_Center) {
  225. self.backImgV.image = [UIImage imageNamed:@"Haircircleoffriends_center"];
  226. }
  227. [[UIApplication sharedApplication].keyWindow addSubview:self];
  228. }
  229. // 点击消失
  230. - (void) tapGesturePressed {
  231. [UIView animateWithDuration:self.animateTime animations:^{
  232. self.alpha = 1.0f;
  233. } completion:^(BOOL finished) {
  234. [UIView animateWithDuration:self.animateTime animations:^{
  235. self.alpha = 0.0f;
  236. } completion:^(BOOL finished) {
  237. [self removeFromSuperview];
  238. }];
  239. }];
  240. }
  241. #pragma mark - inside outside down
  242. - (void) buttonSelectPressed:(UIButton *)button {
  243. self.optionBlock(button.tag, self.listContents[button.tag]);
  244. button.backgroundColor = [UIColor whiteColor];
  245. [self tapGesturePressed];
  246. }
  247. -(void) noPermission:(UIButton *)button{
  248. [g_App showAlert:Localized(@"OrgaVC_PermissionDenied")];
  249. [self tapGesturePressed];
  250. }
  251. - (void) buttonSelectDown:(UIButton *)button {
  252. button.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.5];
  253. }
  254. - (void) buttonSelectOutside:(UIButton *)button {
  255. button.backgroundColor = [UIColor whiteColor];
  256. }
  257. #pragma mark - dealloc
  258. - (void)dealloc {
  259. self.optionBlock = nil;
  260. }
  261. @end