JXAutoSizeImageView.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // JXAutoSizeImageView.m
  3. // shiku_im
  4. //
  5. // Created by MacZ on 2017/8/9.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "JXAutoSizeImageView.h"
  9. @implementation JXAutoSizeImageView
  10. #define _width self.frame.size.width
  11. #define _height self.frame.size.height
  12. #define max_person 16
  13. @synthesize users = _users;
  14. -(id)initWithFrame:(CGRect)frame
  15. {
  16. self = [super initWithFrame:frame];
  17. if (self) {
  18. _users = [[NSMutableArray alloc]init];
  19. _images = [[NSMutableArray alloc]init];
  20. }
  21. return self;
  22. }
  23. -(void)customView {
  24. }
  25. -(BOOL)isMember:(NSString*)userId{
  26. return [_users indexOfObject:userId] != NSNotFound;
  27. }
  28. -(void)add:(NSString*)userId{
  29. if([_users indexOfObject:userId] != NSNotFound )
  30. return;
  31. [_users addObject:userId];
  32. if([_users count]<=max_person){
  33. JXImageView* p;
  34. if([_images count]>[_users count]-1)
  35. p = [_images objectAtIndex:[_users count]-1];
  36. else{
  37. p = [[JXImageView alloc] init];
  38. p.didTouch = @selector(clickUser:);
  39. p.layer.borderWidth = 0.5;
  40. p.layer.borderColor = [UIColor yellowColor].CGColor;
  41. [self addSubview:p];
  42. [_images addObject:p];
  43. }
  44. p.delegate = self.delegate;
  45. [g_server getHeadImageLarge:[_users objectAtIndex:[_users count]-1] userName:nil imageView:p];
  46. [self show];
  47. }
  48. }
  49. -(void)delete:(NSString*)userId{
  50. NSInteger n = [_users indexOfObject:userId];
  51. if(n == NSNotFound )
  52. return;
  53. JXImageView* p;
  54. p = [_images objectAtIndex:n];
  55. [p removeFromSuperview];
  56. [_users removeObjectAtIndex:n];
  57. [_images removeObjectAtIndex:n];
  58. [self show];
  59. }
  60. -(void)show{
  61. CGContextRef context = UIGraphicsGetCurrentContext();
  62. [UIView beginAnimations:nil context:context];
  63. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  64. [UIView setAnimationDuration:1];
  65. [self adjust];
  66. [UIView commitAnimations];
  67. }
  68. -(void)clear{
  69. for(int i=0;i<[_images count];i++){
  70. JXImageView* p = [_images objectAtIndex:i];
  71. p.image = nil;
  72. }
  73. [_images removeAllObjects];
  74. [_users removeAllObjects];
  75. }
  76. -(void)adjust{
  77. int w,h,i,j,n,x,y;
  78. if([_users count]>=13){//13人以上
  79. w = _width/4;
  80. h = _height/4;
  81. x = 0;
  82. y = 0;
  83. n = 0;
  84. for(int i=0;i<4;i++){
  85. for(int j=0;j<4;j++){
  86. if(n>=[_users count])
  87. break;
  88. [self setImage:n x:x y:y w:w h:h];
  89. x += w;
  90. n++;
  91. }
  92. x = 0;
  93. y += h;
  94. if(n>=[_users count])
  95. break;
  96. }
  97. return;
  98. }
  99. if([_users count]>=10){//10人以上
  100. w = _width/4;
  101. h = _height/3;
  102. x = 0;
  103. y = 0;
  104. n = 0;
  105. for(int i=0;i<3;i++){
  106. for(int j=0;j<4;j++){
  107. if(n>=[_users count])
  108. break;
  109. [self setImage:n x:x y:y w:w h:h];
  110. x += w;
  111. n++;
  112. }
  113. x = 0;
  114. y += h;
  115. if(n>=[_users count])
  116. break;
  117. }
  118. return;
  119. }
  120. if([_users count]>=7){//7人以上
  121. w = _width/3;
  122. h = _height/3;
  123. x = 0;
  124. y = 0;
  125. n = 0;
  126. for(int i=0;i<3;i++){
  127. for(int j=0;j<3;j++){
  128. if(n>=[_users count])
  129. break;
  130. [self setImage:n x:x y:y w:w h:h];
  131. x += w;
  132. n++;
  133. }
  134. x = 0;
  135. y += h;
  136. if(n>=[_users count])
  137. break;
  138. }
  139. return;
  140. }
  141. if([_users count]>=5){//5人以上
  142. w = _width/3;
  143. h = _height/2;
  144. x = 0;
  145. y = 0;
  146. n = 0;
  147. for(int i=0;i<2;i++){
  148. for(int j=0;j<3;j++){
  149. if(n>=[_users count])
  150. break;
  151. [self setImage:n x:x y:y w:w h:h];
  152. x += w;
  153. n++;
  154. }
  155. x = 0;
  156. y += h;
  157. if(n>=[_users count])
  158. break;
  159. }
  160. return;
  161. }
  162. if([_users count]==4){//4人
  163. w = _width/2;
  164. h = _height/2;
  165. x = 0;
  166. y = 0;
  167. n = 0;
  168. for(int i=0;i<2;i++){
  169. for(int j=0;j<2;j++){
  170. if(n>=[_users count])
  171. break;
  172. [self setImage:n x:x y:y w:w h:h];
  173. x += w;
  174. n++;
  175. }
  176. y += h;
  177. x = 0;
  178. if(n>=[_users count])
  179. break;
  180. }
  181. return;
  182. }
  183. if([_users count]==3){//3人
  184. w = _width/2;
  185. h = _height/2;
  186. x = 0;
  187. y = 0;
  188. n = 0;
  189. for(int i=0;i<2;i++){
  190. [self setImage:i x:x y:y w:w h:h];
  191. x += w;
  192. }
  193. JXImageView* p=[_images objectAtIndex:2];
  194. p.frame = CGRectMake(w/2, h, w, h);
  195. return;
  196. }
  197. if([_users count]==2){//小于2人
  198. w = _width/2;
  199. h = _height;
  200. x = 0;
  201. y = 0;
  202. for(int i=0;i<[_users count];i++){
  203. [self setImage:i x:x y:y w:w h:h];
  204. x += w;
  205. }
  206. return;
  207. }
  208. if([_users count]==1){//小于1人
  209. w = _width/2;
  210. h = _height;
  211. [self setImage:0 x:w/2 y:0 w:w h:h];
  212. return;
  213. }
  214. }
  215. -(void)setImage:(int)n x:(int)x y:(int)y w:(int)w h:(int)h{
  216. JXImageView* p=[_images objectAtIndex:n];
  217. [g_server getHeadImageLarge:[_users objectAtIndex:n] userName:nil imageView:p];
  218. p.frame = CGRectMake(x, y, w, h);
  219. }
  220. @end