zoukankan      html  css  js  c++  java
  • 拖拽cell 交换cell位置

    #import "ViewController.h"
    
    @interface ViewController ()<UITableViewDelegate ,UITableViewDataSource>
    
    @end
    
    @implementation ViewController
    
    {
        
        UITableView * _tableView;
        
        
        NSMutableArray * _obj;
        
    }
    
    
    
    
    
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        _tableView = [[UITableView alloc]init];
        
        _tableView.frame = self.view.bounds;
        
        [self.view addSubview:_tableView];
       
        _tableView.delegate = self;
        
        _tableView.dataSource = self;
    
    //    [_tableView setEditing:YES animated:YES];
    
        UILongPressGestureRecognizer * longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longpress:)];
        
        [_tableView addGestureRecognizer:longpress];
        
        _obj = [NSMutableArray arrayWithCapacity:10];
        
        for (int i = 0; i<50 ; i++) {
            
            
            
            [_obj addObject:[NSNumber numberWithInt:i]];
    
        }
        
        _tableView.backgroundView =  [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];
       
        
    //    [self tableView:nil moveRowAtIndexPath:nil toIndexPath:nil];
    
    }
    
    
    //-(BOOL)tableView:(UITableView *) tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    //{
    //    //打开编辑
    //    return YES;
    //}
    //- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    //{
    //    //允许移动
    //    return YES;
    //    //return NO;
    //}
    
    
    
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        
        return _obj.count;
    }
    
    
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        
        if (!cell) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            
        }
        cell.textLabel.text = [NSString stringWithFormat:@"%ld" , indexPath.row];
        cell.contentView.backgroundColor = [UIColor lightGrayColor];
        return cell;
    }
    
    - (void)longpress:(id)sender {
        
        
        UILongPressGestureRecognizer * longpress = (UILongPressGestureRecognizer *)sender;
        
        UIGestureRecognizerState state = longpress.state;
        
        CGPoint location = [longpress locationInView:_tableView];
        
        NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
        
        static UIView * snapshot = nil;
        
        static NSIndexPath * sourceIndexPath = nil;
        
        switch (state) {
            case UIGestureRecognizerStateBegan:
                
                if (indexPath) {
                    
                    sourceIndexPath = indexPath;
                    
                    UITableViewCell * cell = [_tableView cellForRowAtIndexPath:indexPath];
                    //截屏快照
                    snapshot = [self customSnapsFromView:cell];
                    
                    
                    __block CGPoint center = cell.center;
                    
                    snapshot.center = center;
                    
                    snapshot.alpha = 0.0;
                    
                    [_tableView addSubview:snapshot];
                    
                    [UIView animateWithDuration:0.25 animations:^{
                        
                       
                        center.y = location.y;
                        
                        snapshot.center = center;
                        
                        snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
                        
                        snapshot.alpha = 0.98;
                        
                        cell.alpha = 0.0f;
                        
                    }completion:^(BOOL finished) {
                        
                        cell.hidden = YES;
                        
                    }];
                    
                }
                
                
                break;
                
            case UIGestureRecognizerStateChanged:{
                
                CGPoint center = snapshot.center;
    
                center.y = location.y;
                
                snapshot.center = center;
                
                if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
                    
                    //交换数组元素的位置
                    [_obj exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];
                    
            //交换cell的位置 [_tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath]; sourceIndexPath = indexPath; } break; } default: { UITableViewCell * cell = [_tableView cellForRowAtIndexPath:indexPath]; [UIView animateWithDuration:0.25 animations:^{ snapshot.center = cell.center; snapshot.transform = CGAffineTransformIdentity; snapshot.alpha = 0.0; cell.alpha = 1.0f; }completion:^(BOOL finished) { cell.hidden = NO; [snapshot removeFromSuperview]; snapshot = nil; }]; sourceIndexPath = nil; break; } } } -(UIView *)customSnapsFromView:(UIView *)inputView { UIView * snapshot = nil; //7.0一下的系统版本 截图快照 if ([[[UIDevice currentDevice]systemVersion]doubleValue]<7.0) { snapshot = [self customSnapShortFromViewEx:inputView]; }else{ snapshot = [inputView snapshotViewAfterScreenUpdates:YES]; } snapshot.layer.masksToBounds = NO; snapshot.layer.cornerRadius = 0.0; snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0); snapshot.layer.shadowRadius = 5.0; snapshot.layer.shadowOpacity = 0.4; return snapshot; } -(UIView *)customSnapShortFromViewEx:(UIView *)inputView { CGSize inSize = inputView.bounds.size;

    // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕分辨率

        UIGraphicsBeginImageContextWithOptions(inSize, NO, [UIScreen mainScreen].scale);
        
        [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
        
        UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
        UIImageView * snapshot = [[UIImageView alloc]initWithImage:image];
        
        return snapshot;
        
        
    }
    
    
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        
    }
    
    
    
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        return 64.0f;
    }
    
    @end
    

      

  • 相关阅读:
    Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
    旋转二维数组
    replace empty char with new string,unsafe method和native implementation的性能比较
    判断一字符串是否可以另一字符串重新排列而成
    移除重复字符的几个算法简单比较
    也来纠结一下字符串翻转
    判断重复字符存在:更有意义一点
    程序员常去网站汇总
    sublime
    针对程序集 'SqlServerTime' 的 ALTER ASSEMBLY 失败,因为程序集 'SqlServerTime' 未获授权(PERMISSION_SET = EXTERNAL_ACCESS)
  • 原文地址:https://www.cnblogs.com/yuwei0911/p/5688887.html
Copyright © 2011-2022 走看看