123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- //
- // JXAutoSizeImageView.m
- // shiku_im
- //
- // Created by MacZ on 2017/8/9.
- // Copyright © 2017年 Reese. All rights reserved.
- //
- #import "JXAutoSizeImageView.h"
- @implementation JXAutoSizeImageView
- #define _width self.frame.size.width
- #define _height self.frame.size.height
- #define max_person 16
- @synthesize users = _users;
- -(id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- _users = [[NSMutableArray alloc]init];
- _images = [[NSMutableArray alloc]init];
- }
- return self;
- }
- -(void)customView {
- }
- -(BOOL)isMember:(NSString*)userId{
- return [_users indexOfObject:userId] != NSNotFound;
- }
- -(void)add:(NSString*)userId{
- if([_users indexOfObject:userId] != NSNotFound )
- return;
- [_users addObject:userId];
- if([_users count]<=max_person){
- JXImageView* p;
- if([_images count]>[_users count]-1)
- p = [_images objectAtIndex:[_users count]-1];
- else{
- p = [[JXImageView alloc] init];
- p.didTouch = @selector(clickUser:);
- p.layer.borderWidth = 0.5;
- p.layer.borderColor = [UIColor yellowColor].CGColor;
- [self addSubview:p];
- [_images addObject:p];
- }
- p.delegate = self.delegate;
- [g_server getHeadImageLarge:[_users objectAtIndex:[_users count]-1] userName:nil imageView:p];
- [self show];
- }
- }
- -(void)delete:(NSString*)userId{
- NSInteger n = [_users indexOfObject:userId];
- if(n == NSNotFound )
- return;
- JXImageView* p;
- p = [_images objectAtIndex:n];
- [p removeFromSuperview];
- [_users removeObjectAtIndex:n];
- [_images removeObjectAtIndex:n];
- [self show];
- }
- -(void)show{
- CGContextRef context = UIGraphicsGetCurrentContext();
- [UIView beginAnimations:nil context:context];
- [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
- [UIView setAnimationDuration:1];
-
- [self adjust];
- [UIView commitAnimations];
- }
- -(void)clear{
- for(int i=0;i<[_images count];i++){
- JXImageView* p = [_images objectAtIndex:i];
- p.image = nil;
- }
- [_images removeAllObjects];
- [_users removeAllObjects];
- }
- -(void)adjust{
- int w,h,i,j,n,x,y;
- if([_users count]>=13){//13人以上
- w = _width/4;
- h = _height/4;
- x = 0;
- y = 0;
- n = 0;
- for(int i=0;i<4;i++){
- for(int j=0;j<4;j++){
- if(n>=[_users count])
- break;
- [self setImage:n x:x y:y w:w h:h];
- x += w;
- n++;
- }
- x = 0;
- y += h;
- if(n>=[_users count])
- break;
- }
- return;
- }
- if([_users count]>=10){//10人以上
- w = _width/4;
- h = _height/3;
- x = 0;
- y = 0;
- n = 0;
- for(int i=0;i<3;i++){
- for(int j=0;j<4;j++){
- if(n>=[_users count])
- break;
- [self setImage:n x:x y:y w:w h:h];
- x += w;
- n++;
- }
- x = 0;
- y += h;
- if(n>=[_users count])
- break;
- }
- return;
- }
- if([_users count]>=7){//7人以上
- w = _width/3;
- h = _height/3;
- x = 0;
- y = 0;
- n = 0;
- for(int i=0;i<3;i++){
- for(int j=0;j<3;j++){
- if(n>=[_users count])
- break;
- [self setImage:n x:x y:y w:w h:h];
- x += w;
- n++;
- }
- x = 0;
- y += h;
- if(n>=[_users count])
- break;
- }
- return;
- }
- if([_users count]>=5){//5人以上
- w = _width/3;
- h = _height/2;
- x = 0;
- y = 0;
- n = 0;
- for(int i=0;i<2;i++){
- for(int j=0;j<3;j++){
- if(n>=[_users count])
- break;
- [self setImage:n x:x y:y w:w h:h];
- x += w;
- n++;
- }
- x = 0;
- y += h;
- if(n>=[_users count])
- break;
- }
- return;
- }
-
- if([_users count]==4){//4人
- w = _width/2;
- h = _height/2;
- x = 0;
- y = 0;
- n = 0;
- for(int i=0;i<2;i++){
- for(int j=0;j<2;j++){
- if(n>=[_users count])
- break;
- [self setImage:n x:x y:y w:w h:h];
- x += w;
- n++;
- }
- y += h;
- x = 0;
- if(n>=[_users count])
- break;
- }
- return;
- }
-
- if([_users count]==3){//3人
- w = _width/2;
- h = _height/2;
- x = 0;
- y = 0;
- n = 0;
- for(int i=0;i<2;i++){
- [self setImage:i x:x y:y w:w h:h];
- x += w;
- }
- JXImageView* p=[_images objectAtIndex:2];
- p.frame = CGRectMake(w/2, h, w, h);
- return;
- }
-
- if([_users count]==2){//小于2人
- w = _width/2;
- h = _height;
- x = 0;
- y = 0;
- for(int i=0;i<[_users count];i++){
- [self setImage:i x:x y:y w:w h:h];
- x += w;
- }
- return;
- }
- if([_users count]==1){//小于1人
- w = _width/2;
- h = _height;
- [self setImage:0 x:w/2 y:0 w:w h:h];
- return;
- }
- }
- -(void)setImage:(int)n x:(int)x y:(int)y w:(int)w h:(int)h{
- JXImageView* p=[_images objectAtIndex:n];
- [g_server getHeadImageLarge:[_users objectAtIndex:n] userName:nil imageView:p];
- p.frame = CGRectMake(x, y, w, h);
- }
- @end
|