如何在表之间复制单元格作为脉冲应用程序 [英] How to copy cells between tables as the pulse app

查看:117
本文介绍了如何在表之间复制单元格作为脉冲应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在2个表视图之间复制单元格



我有一个或多或少的工作解决方案。但是,这并不顺利,我想做一些类似于脉冲应用程序重新排序Feed选项...



脉冲工作方式特别的是重新排序动画与在细胞的正常排序中,但是细胞可以在表之间移动。



这是我现在所在的:



(code $ c) - (BoardColumnView *)addColumn:(NSString *)标题类型:(ColumnType)type {
BoardColumnView * col = [BoardColumnView createColumn:title]

[self.columns addObject:col];

//向表格视图添加捏捏手势识别器。
UILongPressGestureRecognizer * draggingRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(moveActionGestureRecognizerStateChanged :)
];

draggingRecognizer.minimumPressDuration = 0.5;
drageringRecognizer.delegate = self;

[col.tableView addGestureRecognizer:draggingRecognizer];
[draggingRecognizer release];


return col;
}

#pragma mark -
#pragma mark UIGestureRecognizer代理/操作

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
ALog(@检测到拖动);
返回YES;
}

- (void)moveActionGestureRecognizerStateChanged:(UIGestureRecognizer *)识别器
{
开关(识别器状态)
{
默认值:
case UIGestureRecognizerStateFailed:
// do nothing
break;

case UIGestureRecognizerStatePossible:
case UIGestureRecognizerStateCancelled:
{
ALog(@Cancellled);

[UIView beginAnimations:@SnapBackcontext:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(finishedSnap:finished:context :)];

CGRect f = self.dragView.frame;
f.origin = _dragOriginCellOrigin;
self.dragView.frame = f;

[UIView commitAnimations];

break;
}
case UIGestureRecognizerStateEnded:
{
//将真实单元格移动到位
[UIView beginAnimations:@SnapToPlacecontext:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(finishedSnap:finished:context :)];

CGRect r = self.dragView.frame; // [self.content rectForItemAtIndex:_emptyCellIndex];
CGRect f = self.dragView.frame;
f.origin.x = r.origin.x + floorf((r.size.width - f.size.width)* 0.5);
f.origin.y = r.origin.y + floorf((r.size.height - f.size.height)* 0.5);
NSLog(@手势结束 - 移动到%@,NSStringFromCGRect(f));
self.dragView.frame = f;

self.dragView.transform = CGAffineTransformIdentity;
self.dragView.alpha = 1.0;

[UIView commitAnimations];
break;
}

case UIGestureRecognizerStateBegan:
{
ALog(@Start move ..);
//在当前位置找到单元格并将其复制到我们的主视图中,应用一些变换
_lastColumn =(BoardColumnView *)[(UITableView *)recognitionizer.view superview];
_targetColumn = _lastColumn;

if(_lastIndexPath){
[_lastIndexPath release];
_lastIndexPath = nil;
}

_lastIndexPath = [_lastColumn.tableView indexPathForRowAtPoint:[recognitionizer locationInView:_lastColumn]];

UITableViewCell * sourceCell = [_lastColumn.tableView cellForRowAtIndexPath:_lastIndexPath];

CGRect frame = [_lastColumn convertRect:sourceCell.frame fromView:self.content];

self.dragView = [[[UIImageView alloc] initWithImage:[sourceCell screenshot]] autorelease];
self.dragView.opaque = YES;
self.dragView.backgroundColor = _lastColumn.backgroundColor;

self.dragView.center = [识别器locationInView:self.view];
//self.dragView.cornerRadius = 8; //如果你喜欢圆角
self.dragView.layer.shadowOffset = CGSizeMake(-5,2);
self.dragView.layer.shadowRadius = 5;
self.dragView.layer.shadowOpacity = 0.5;

[content addSubview:self.dragView];

[content bringSubviewToFront:self.dragView];
//获取有关此单元格的一些信息
_dragOriginCellOrigin = frame.origin;

[UIView beginAnimations:@context:NULL];
[UIView setAnimationDuration:0.2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

//变换 - 更大,稍微透明
self.dragView.transform = CGAffineTransformMakeScale(1.2,1.2);
self.dragView.alpha = 0.7;

[UIView commitAnimations];

/ * //重新加载下面的网格以获取空单元格
[self.content reloadItemsAtIndices:[NSIndexSet indexSetWithIndex:index]
withAnimation:AQGridViewItemAnimationNone];
* /
break;
}

case UIGestureRecognizerStateChanged:
{
//更新draging单元格位置
self.dragView.center = [识别器locationInView:self.view];

//确定正确的列列表esta flotando ..
CGRect全局;

if(_targetColumn){
_targetColumn = nil;
}
for(BoardColumnView * col in self.columns){
UITableView * table = col.tableView;
global = [table convertRect:self.dragView.frame fromView:content];

if([table pointInside:global.origin withEvent:nil]){
ALog([NSString stringWithFormat:@Esta sobre la tabla%@,col.column.name]) ;
_targetColumn = col;
break;
}
}

如果(!_targetColumn)
{
ALog(@Esta en zona muerta);
}

break;
}
}


- (void)finishedSnap :( NSString *)animationID已完成:(NSNumber *)完成上下文:(void *)context
{
if(_targetColumn&& _lastIndexPath){
ALog(@Moviendo a nuevo destino);

CGRect frame = [_targetColumn convertRect:self.dragView.frame fromView:self.content];
NSIndexPath * destPath = [_targetColumn.tableView indexPathForRowAtPoint:frame.origin];

[UIView beginAnimations:@context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context :)];

self.dragView.backgroundColor = _targetColumn.backgroundColor;

//插入tareas en destino ...

_lastColumn.totalRows - = 1;
_targetColumn.totalRows + = 1;

[_lastColumn.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:_lastIndexPath] withRowAnimation:UITableViewRowAnimationTop];

if(destPath){
[_targetColumn.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:destPath] withRowAnimation:UITableViewRowAnimationBottom];
} else {
[_targetColumn.tableView reloadData];
}



[UIView commitAnimations];
} else {
ALog(@cancelado el moviento,regresar a home);

UITableViewCell * sourceCell = [_lastColumn.tableView cellForRowAtIndexPath:_lastIndexPath];
if(sourceCell){

[UIView beginAnimations:@Backcontext:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context :)];

//self.dragView.transform = CGAffineTransformIdentity;
self.dragView.frame = [self.content convertRect:sourceCell.frame fromView:_lastColumn];
//self.dragView.alpha = 0.0;

[UIView commitAnimations];
}
}


- (void)animationDidStop:(id)animationID已完成:(BOOL)标志上下文:(id)context {
//关闭我们的单元格
ALog(@Eliminando drag view);
[self.dragView removeFromSuperview];
self.dragView = nil;

_lastColumn = nil;
_targetColumn = nil;

if(_lastIndexPath){
_lastIndexPath = nil;
}
}

更新:我创建了一个开源项目,以显示我迄今为止的进展:



https://bitbucket.org/elmalabarista/dragdroptableviews

解决方案

尝试这些链接,如何做到这一点。





这可能有助于您。


I need to copy cells between 2 table views.

I have a more-or-less working solution. However, it is not smooth, and I would love to do something similar to the pulse app reorder feeds option...

Something special in the way that pulse works, is that the reorder animation is the same as in a normal reorder of cells, but still the cell can move between tables.

This is what I have now:

- (BoardColumnView *) addColumn:(NSString *)title type:(ColumnType)type{
    BoardColumnView *col = [BoardColumnView createColumn:title];

    [self.columns addObject:col];

    // Add a pinch gesture recognizer to the table view.
    UILongPressGestureRecognizer* draggingRecognizer = [[UILongPressGestureRecognizer alloc] 
                                                    initWithTarget:self 
                                                    action:@selector(moveActionGestureRecognizerStateChanged:)
                                                   ];

    draggingRecognizer.minimumPressDuration = 0.5;
    draggingRecognizer.delegate = self;

    [col.tableView addGestureRecognizer:draggingRecognizer];
    [draggingRecognizer release]; 


    return col;
}

#pragma mark -
#pragma mark UIGestureRecognizer Delegate/Actions

- (BOOL) gestureRecognizerShouldBegin: (UIGestureRecognizer *) gestureRecognizer
{
    ALog(@"Drag detected");
    return YES;
}

- (void) moveActionGestureRecognizerStateChanged: (UIGestureRecognizer *) recognizer
{
    switch ( recognizer.state )
    {
        default:
        case UIGestureRecognizerStateFailed:
            // do nothing
            break;

        case UIGestureRecognizerStatePossible:
        case UIGestureRecognizerStateCancelled:
        {
            ALog(@"Canceled");

            [UIView beginAnimations: @"SnapBack" context: NULL];
            [UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
            [UIView setAnimationDuration: 0.5];
            [UIView setAnimationDelegate: self];
            [UIView setAnimationDidStopSelector: @selector(finishedSnap:finished:context:)];

            CGRect f = self.dragView.frame;
            f.origin = _dragOriginCellOrigin;
            self.dragView.frame = f;

            [UIView commitAnimations];

            break;
        }
        case UIGestureRecognizerStateEnded:
        {
            // move the real cell into place
            [UIView beginAnimations: @"SnapToPlace" context: NULL];
            [UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
            [UIView setAnimationDuration: 0.5];
            [UIView setAnimationDelegate: self];
            [UIView setAnimationDidStopSelector: @selector(finishedSnap:finished:context:)];

            CGRect r = self.dragView.frame;//[self.content rectForItemAtIndex: _emptyCellIndex];
            CGRect f = self.dragView.frame;
            f.origin.x = r.origin.x + floorf((r.size.width - f.size.width) * 0.5);
            f.origin.y = r.origin.y + floorf((r.size.height - f.size.height) * 0.5);
            NSLog( @"Gesture ended-- moving to %@", NSStringFromCGRect(f) );
            self.dragView.frame = f;

            self.dragView.transform = CGAffineTransformIdentity;
            self.dragView.alpha = 1.0;

            [UIView commitAnimations];
            break;
        }

        case UIGestureRecognizerStateBegan:
        {           
            ALog(@"Start move..");
            // find the cell at the current point and copy it into our main view, applying some transforms
            _lastColumn = (BoardColumnView *)[(UITableView *)recognizer.view superview];
            _targetColumn = _lastColumn;

            if (_lastIndexPath) {
                [_lastIndexPath release];
                _lastIndexPath = nil;
            }

            _lastIndexPath = [_lastColumn.tableView indexPathForRowAtPoint:[recognizer locationInView: _lastColumn]];

            UITableViewCell *sourceCell = [_lastColumn.tableView cellForRowAtIndexPath:_lastIndexPath];

            CGRect frame = [_lastColumn convertRect: sourceCell.frame fromView: self.content];

            self.dragView = [[[UIImageView alloc] initWithImage:[sourceCell screenshot]] autorelease];
            self.dragView.opaque = YES;
            self.dragView.backgroundColor = _lastColumn.backgroundColor;

            self.dragView.center = [recognizer locationInView: self.view];
            //self.dragView.cornerRadius = 8; // if you like rounded corners
            self.dragView.layer.shadowOffset = CGSizeMake(-5, 2);
            self.dragView.layer.shadowRadius = 5;
            self.dragView.layer.shadowOpacity = 0.5;

            [content addSubview: self.dragView];

            [content bringSubviewToFront:self.dragView];
            // grab some info about the origin of this cell
            _dragOriginCellOrigin = frame.origin;

            [UIView beginAnimations: @"" context: NULL];
            [UIView setAnimationDuration: 0.2];
            [UIView setAnimationCurve: UIViewAnimationCurveEaseOut];

            // transformation-- larger, slightly transparent
            self.dragView.transform = CGAffineTransformMakeScale( 1.2, 1.2 );
            self.dragView.alpha = 0.7;

            [UIView commitAnimations];

            /* // reload the grid underneath to get the empty cell in place
            [self.content reloadItemsAtIndices: [NSIndexSet indexSetWithIndex: index]
                              withAnimation: AQGridViewItemAnimationNone];
            */
            break;
        }

        case UIGestureRecognizerStateChanged:
        {
            // update draging cell location
            self.dragView.center = [recognizer locationInView: self.view];

            //Determinar sobre que columna esta flotando..
            CGRect global;

            if (_targetColumn) {
                _targetColumn = nil;
            }
            for (BoardColumnView *col in self.columns) {
                UITableView *table = col.tableView;
                global = [table convertRect:self.dragView.frame fromView:content];

                if ([table pointInside:global.origin withEvent:nil]) {
                    ALog([NSString stringWithFormat: @"Esta sobre la tabla %@", col.column.name]);
                    _targetColumn = col;
                    break;
                }
            }

            if ( !_targetColumn )
            {                
                ALog(@"Esta en zona muerta");
            }

            break;
        }
    }
}

- (void) finishedSnap: (NSString *) animationID finished: (NSNumber *) finished context: (void *) context
{       
    if (_targetColumn && _lastIndexPath) {      
        ALog(@"Moviendo a nuevo destino");

        CGRect frame = [_targetColumn convertRect: self.dragView.frame fromView: self.content];     
        NSIndexPath *destPath  = [_targetColumn.tableView indexPathForRowAtPoint: frame.origin];

        [UIView beginAnimations: @"" context: NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

        self.dragView.backgroundColor = _targetColumn.backgroundColor;

        //Si inserta entre tareas en destino...

        _lastColumn.totalRows -= 1;
        _targetColumn.totalRows += 1;

        [_lastColumn.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:_lastIndexPath] withRowAnimation:UITableViewRowAnimationTop];

        if (destPath) {         
            [_targetColumn.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:destPath] withRowAnimation:UITableViewRowAnimationBottom];
        } else {
            [_targetColumn.tableView reloadData];
        }



        [UIView commitAnimations];
    } else {        
        ALog(@"Cancelado el moviento, regresar a home");

        UITableViewCell * sourceCell = [_lastColumn.tableView cellForRowAtIndexPath:_lastIndexPath];        
        if (sourceCell) {

            [UIView beginAnimations: @"Back" context: NULL];
            [UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
            [UIView setAnimationDuration: 0.5];
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

            //self.dragView.transform = CGAffineTransformIdentity;
            self.dragView.frame = [self.content convertRect: sourceCell.frame fromView: _lastColumn];
            //self.dragView.alpha = 0.0;

            [UIView commitAnimations];      
        }
    }
}

- (void)animationDidStop:(id)animationID finished:(BOOL)flag context:(id)context {
    // dismiss our copy of the cell
    ALog(@"Eliminando drag view");
    [self.dragView removeFromSuperview];
    self.dragView = nil;

    _lastColumn = nil;
    _targetColumn = nil;

    if (_lastIndexPath) {
        _lastIndexPath = nil;
    }   
}

UPDATE: I've created an open source project to show my progress so far:

https://bitbucket.org/elmalabarista/dragdroptableviews

解决方案

Try these links which gives a sample on how to do this.

This may help you.

这篇关于如何在表之间复制单元格作为脉冲应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆