宽容他人,放过自己。

图片位置的交换

Posted on By anchoriteFili

752372-20150906163039311-2108948739.png

图片交换的实现,分为页面数据交换和服务器数据交换

服务器数据交换:获取所有的服务器信息,根据页面图片位置的变化来进服务器信息的位置的改变

@property (nonatomic,retain) NSMutableDictionary *informationDic; //存储所有从服务器得到的信息

#pragma mark informationDic懒加载
- (NSMutableDictionary *)informationDic {
    if (!_informationDic) {
        _informationDic = [NSMutableDictionary dictionary];
    }
    return _informationDic;
}

[self.informationDic setObject:dic[@"imgid"] forKey:dic[@"photolocindex"]]; //获取服务器中图片的图片位置和imageID

#pragma mark 调换字典中的两个位置
    NSNumber *beginObject = [self.informationDic objectForKey:[NSNumber numberWithInteger:beginLoc]];
    NSNumber *endObject = [self.informationDic objectForKey:[NSNumber numberWithInteger:endLoc]];
    
    [self.informationDic setObject:endObject forKey:[NSNumber numberWithInteger:beginLoc]];
    [self.informationDic setObject:beginObject forKey:[NSNumber numberWithInteger:endLoc]];

#pragma mark 图片调换位置接口
- (void)exchangePicture
{
#pragma mark 固定数据
    NSString *t = [BaseViewController millisecondsDifferential];
    NSString *ImgId = [NSString string];
    NSString *PhotoInd = [NSString string];
#pragma mark 变化部分
    NSArray *keys = [self.informationDic allKeys];
    for (NSNumber *key  in keys) {
        
        ImgId = self.informationDic[key];
        PhotoInd = [NSString stringWithFormat:@"%@",key];
        
        NSString *jiami = [BaseViewController md5:[NSString stringWithFormat:@"t=%@&ImgID=%@&PhotoIndex=%@&UserId=%ld",t,ImgId,PhotoInd,_userid]];
        NSString *jiami1 = [BaseViewController Keymd5:jiami];
        NSString *auth = [BaseViewController md5:jiami1];
        
        NSString *strUrl = [NSString stringWithFormat:@"%@%@%@&%@%@&%@%@&%@%@&%@%ld",KTHttp,kExchangPicture,t,@"auth=",auth,@"ImgID=",ImgId,@"PhotoIndex=",PhotoInd,@"UserId=",_userid];
        
        [NetManager requestDataWithUrlString:strUrl finished:^(id responseObj) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableContainers error:nil];
            if (!_letfVC) {
                _letfVC = [[LeftVC alloc] init];
            }
            [_letfVC personHomepage];
            NSLog(@"%@",dic);
        } failed:^(NSString *errorMsg) {
            //NSLog(@"%@",errorMsg);
        }];
    }
}

页面图片的交换,两种方式:

  • 1> 第一种直接交换两个图片的image,而本身imageview不变,这样的话,所有的tag值都可以写死,只需要改变imageDic(图片位置和图片一一对应的数据),根据index来对图片进行重新赋值,刷新talbeView
  • 2> 直接改变所有imageView的frame,也就是所有的imageView都可以随意移动,对整个局面来说没有太大的影响,用其tag值和本身的image来决定一切。
- (void)exchangePhoto:(NSInteger)beginLoc andEndloc:(NSInteger)endLoc; //代理方法,对外传输两个交换的imageView的坐标,只需根据坐标将两个坐标对应的imageId进行交换即可
@property (nonatomic,retain) NSMutableDictionary *imageDic; //存储申请下来的图片

#pragma mark 懒加载imageDic
- (NSMutableDictionary *)imageDic {
    if (!_imageDic) {
        _imageDic = [NSMutableDictionary dictionary];
    }
    return _imageDic;
}

#pragma mark 根据接口获取所有的image,将对应的坐标和image存到imageDic中
- (void)imagesWithImageUrlDic:(NSMutableDictionary *)imageUrlDic {
    
    UIImageView *imageView = [[UIImageView alloc] init];
    
    NSArray *keys = [imageUrlDic allKeys];
    if (keys.count) {
        
        for (NSNumber *key in keys) {
            NSInteger index = key.integerValue;
            
            if (index == 1) {
                [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrlDic[key]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    [self.imageDic setObject:image forKey:key];
                }];
            } else if (index==2) {
                [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrlDic[key]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    [self.imageDic setObject:image forKey:key];
                }];
            } else if (index==3) {
                [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrlDic[key]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    [self.imageDic setObject:image forKey:key];
                }];
            } else if (index==4) {
                [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrlDic[key]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    [self.imageDic setObject:image forKey:key];
                }];
            } else if (index==5) {
                [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrlDic[key]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    [self.imageDic setObject:image forKey:key];
                }];
            } else if (index==6) {
                [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrlDic[key]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    [self.imageDic setObject:image forKey:key];
                }];
            }
        }
    }
    [self.tableView reloadData];
}

#pragma mark 重写imageDic的setter方法,对所有的ImageView进行赋值,根据tag值赋值,不是死值
- (void)setImageDic:(NSMutableDictionary *)imageDic {
    
    for (int i=0;i<6;i++){
        isUrlImage[i] = NO;
    }
    isUrlImage[0] = YES;
    //初始时全部添加加号图片
    _firstImageView.image = [UIImage imageNamed:@"2.png"];
    _secondImageView.image = [UIImage imageNamed:@"2.png"];
    _thirthImageView.image = [UIImage imageNamed:@"2.png"];
    _forthImageView.image = [UIImage imageNamed:@"2.png"];
    _fifthImageView.image = [UIImage imageNamed:@"2.png"];
    _sixthImageView.image = [UIImage imageNamed:@"2.png"];
    
    NSArray *keys = [imageDic allKeys];
    
    for (NSNumber *key in keys) {
        NSInteger index = key.integerValue;
        if (index == self.firstImageView.tag-1000) {
            isUrlImage[0] = YES;
            self.firstImageView.image = imageDic[key];
        } else if (index==self.secondImageView.tag-1000){
            isUrlImage[1] = YES;
            self.secondImageView.image = imageDic[key];
        } else if (index==self.thirthImageView.tag-1000){
            isUrlImage[2] = YES;
            self.thirthImageView.image = imageDic[key];
        } else if (index==self.forthImageView.tag-1000){
            isUrlImage[3] = YES;
            self.forthImageView.image = imageDic[key];
        } else if (index==self.fifthImageView.tag-1000){
            isUrlImage[4] = YES;
            self.fifthImageView.image = imageDic[key];
        } else if (index==self.sixthImageView.tag-1000){
            isUrlImage[5] = YES;
            self.sixthImageView.image = imageDic[key];
        }
    }
}

#pragma mark 拖动事件,这个,我要好好的研究研究,看看手势方法
- (void)dragButton:(UIPanGestureRecognizer *)pan
{
    
    if (!isUrlImage[pan.view.tag-1001]) {
        return;
    }
    //获取手势在该视图上得偏移量
    CGPoint translation = [pan translationInView:self.contentView];
    
    if (pan.state == UIGestureRecognizerStateBegan)
    {
        [self.contentView bringSubviewToFront:pan.view];
#warning 如果点击的是第一张图片
        if (pan.view.tag == 1001) {
            pan.view.transform = CGAffineTransformScale(pan.view.transform, 0.25, 0.25);
            return;
        }else
            pan.view.transform = CGAffineTransformScale(pan.view.transform, 0.5, 0.5);
    }
    else if(pan.state == UIGestureRecognizerStateChanged)
    {
        //使拖动的view跟随手势移动
        pan.view.center = CGPointMake(pan.view.center.x + translation.x,
                                      pan.view.center.y + translation.y);
        
        [pan setTranslation:CGPointZero inView:self.contentView];
        
        for (int i = 0; i< self.myRects.count; i++)
        {
            UIButton * btn = self.myRects[i];
            NSString* tmprect = self.frames[i];
            
            if (CGRectContainsPoint(CGRectFromString(tmprect), pan.view.center))
            {
                if (!isUrlImage[btn.tag-1001]) {
                    return;
                }
                _tmptag = btn.tag;
                return;
            }
        }
    } //上面的移动应该是没有问题的,但是停下来的index应该有问题
    else if (pan.state == UIGestureRecognizerStateEnded)
    {
        
        [UIView animateWithDuration:0.3 animations:^
         {
             UIImageView * btn = self.myRects[_tmptag-1001];
             beginLoc = pan.view.tag - 1000;
             endloc = btn.tag -1000;
             
             if (btn.tag == 1001&&btn.tag!=pan.view.tag) {
                 btn.transform = CGAffineTransformScale(btn.transform, 0.5, 0.5);
                 
                 pan.view.transform = CGAffineTransformScale(pan.view.transform, 4, 4);
             }
             //获取需要交换的两个view的frame,并交换
             NSString * rect1 = self.frames[btn.tag-1001];
             NSString * rect2 = self.frames[pan.view.tag-1001];
//             这里,进行了frame的交换
             pan.view.frame = CGRectFromString(rect1);
             btn.frame = CGRectFromString(rect2);
             
//             这里进行tag值的交换
             NSInteger temp = pan.view.tag;
             pan.view.tag = btn.tag;
             btn.tag = temp;
//             btn的tag值等于1001并且不能与点击view的tag值,则交换位置
             if (btn.tag == 1001&&btn.tag!=pan.view.tag) {
                 btn.transform = CGAffineTransformScale(btn.transform, 2, 2);
                 pan.view.transform = CGAffineTransformScale(pan.view.transform, 2, 2);
                 pan.view.frame = CGRectFromString(rect1);
                 btn.frame =CGRectMake(0, 0, (Swidth/3)*2, (Swidth/3)*2);
                 
             }
//             如果两个都不是第一张图片
             if (pan.view.tag!= 1001&& btn.tag != 1001) {
                 pan.view.transform = CGAffineTransformScale(pan.view.transform, 2, 2);
                 pan.view.frame = CGRectFromString(rect1);
             } //如果两个tag值相等
             else if (btn.tag==pan.view.tag) {
                 pan.view.transform = CGAffineTransformScale(btn.transform, 4, 4);
                 pan.view.frame =CGRectMake(0, 0, (Swidth/3)*2, (Swidth/3)*2);
             }
             [self.myRects exchangeObjectAtIndex:pan.view.tag-1001 withObjectAtIndex:btn.tag-1001];
             
         } completion:^(BOOL finished)
         {
             [_delegate exchangePhoto:endloc andEndloc:beginLoc]; //这里是相互交换的两个图片的位置
             NSLog(@"已交换");
         }];
    }
}