2011-03-15 13 views
9

Necesito copiar celdas between 2 table views.Cómo copiar celdas entre tablas como la aplicación de pulso

Tengo una solución de trabajo más o menos. Sin embargo, no es sencillo, y me gustaría hacer algo similar a la opción de reordenar pulse app ...

Algo especial en la forma en que funciona el pulso, es que la animación de reordenación es la misma que en un reordenamiento normal de celdas, pero aún la celda puede moverse entre tablas.

Esto es lo que tengo ahora:

- (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; 
    } 
} 

ACTUALIZACIÓN: He creado un proyecto de código abierto para mostrar mi progreso hasta el momento:

https://bitbucket.org/elmalabarista/dragdroptableviews

+0

Te recomiendo que leas estos tutoriales sobre la aplicación de pulso similar http://www.raywenderlich.com/4680/how-to-make-an-interface-with-horizontal-tables-like-the-pulse-news-app -part-1 http://www.raywenderlich.com/4723/how-to-make-an-interface-con-horizontal-tables-like-the-pulse-news-app-part-2 y este es el OpenSource http://www.raywenderlich.com/downloads/HorizontalTables2.zip ¡Disfrútalo! –

+0

posible duplicado de [ipad: arrastre una UITableViewCell desde un UITableViewController y suéltelo en otro] (http://stackoverflow.com/questions/6506946/ipad-drag-a-uitableviewcell-from-one-uitableviewcontroller-and-drop- it-into-ano) – CodaFi

Respuesta

Cuestiones relacionadas