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
    

      

  • 相关阅读:
    Postman使用教程
    CAD和ArcGIS转换 矢量配准
    SAP CRM Advanced search和Simple search里Max hit表现行为的差异
    SAP CRM Product simple search的启用步骤
    如何快速定位SAP CRM订单应用(Order Application)错误消息抛出的准确位置
    如何动态修改SAP CRM WebClient UI表格栏的宽度
    如何在SAP CRM WebClient UI里创建web service并使用ABAP消费
    如何处理SAP CRM Web Service错误
    如何使用SAP CRM WebClient UI实现一个类似新浪微博的字数统计器
    如何开启SAP CRM基于WORD模板创建附件的功能
  • 原文地址:https://www.cnblogs.com/yuwei0911/p/5688887.html
Copyright © 2011-2022 走看看