2011-05-08 6 views
23

Creo una aplicación de iPhone basada en uitableview y uitabbar. En cada celda de la tabla, tengo un "agregar al botón favorito". Cuando presioné este botón, quiero hacer que la celda "salte" de su posición al elemento favorito de la tabbar (igual que el efecto de descarga en installous)Obtener la posición de UITableviewCell desde el área o ventana "visible"

El efecto funciona bien si estoy en la parte superior de la celda 10 , pero si me desplazo en la tabla Vista, el efecto no es bueno porque la posición de inicio se calcula desde el tamaño de uitableview.

Es posible conocer la posición de una celda desde el área visible. ?

Ejemplo: para la posición de la cuadragésima celda es x: 0, y: 1600, w: 320, y: 50, quiero la posición desde la parte superior de la pantalla no desde la parte superior de la vista algo así x : 0, y: 230, w: 320, y: 50

Respuesta

75

Sí, puede usar rectForRowAtIndexPath:, solo tendrá que pasar en el índice de la fila en la que ha hecho clic.

rectForRowAtIndexPath:

Devuelve el área de dibujo para una fila identificado por la ruta del índice.

...

EDITAR

Conversión:

CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath]; 
CGRect rectInSuperview = [tableView convertRect:rectInTableView toView:[tableView superview]]; 
+0

Para el RectForRowAtIndexPath 20 células Devuélveme x: 0, y: 1200 y quiero la posición desde la parte superior de la vista, (desde la barra de estado) por lo que debe estar entre 20 y aproximadamente 367 (tamaño de la vista) – Mickael

+0

@Benja Min Frolicher, lo siento, he actualizado mi respuesta, faltaba la conversión. –

+0

Gracias Nick, el código en tu comentario funciona bien! – Mickael

-1

Así que aquí está el código que utilicé: (se puede optimizar estoy seguro):

Primero agregue la propiedad NSindexPath ** a su celda personalizada.
* implementar esto en su controlador tableview: *

-(void)addCellToFavorisWithAnimation:(ResultSearchOffreCell*)cell{ 
/* Generate image from cell 
----------------------------------------------------------*/ 
UIGraphicsBeginImageContextWithOptions(cell.bounds.size, cell.opaque, [[UIScreen mainScreen] scale]); 
[cell.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

// Launch first animation (go to top-right) 
//----------------------------------------------------------*/ 
CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:cell.indexPath]; 
CGRect rectInSuperview = [self.tableView convertRect:rectInTableView toView:[self.tableView superview]]; 

cellAnimationContainer = [[UIImageView alloc] initWithFrame:rectInSuperview]; 

[cellAnimationContainer setFrame:CGRectMake(cellAnimationContainer.frame.origin.x, cellAnimationContainer.frame.origin.y+50 , cellAnimationContainer.frame.size.width, cellAnimationContainer.frame.size.height)]; 

[cellAnimationContainer setImage:img]; 

AppDelegate_Shared *appDelegate = [UIApplication sharedApplication].delegate; 
[appDelegate.window addSubview:cellAnimationContainer]; 
[UIView beginAnimations:@"addFavorite-Step1" context:nil]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
[UIView setAnimationDuration:0.2]; 
[cellAnimationContainer setFrame:CGRectMake(20,cellAnimationContainer.frame.origin.y-10, cellAnimationContainer.frame.size.width, cellAnimationContainer.frame.size.height)]; 
[cellAnimationContainer setAlpha:0.7]; 
[UIView commitAnimations]; 
} 
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ 


/* Launch second animation => go to favoris Tab and remove from self.view 
---------------------------------------------------------------------------*/ 
if([anim isEqual:@"addFavorite-Step1"]) 
{ 
    [UIView beginAnimations:@"addFavorite-Step2" context:nil]; 
    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    [UIView setAnimationDuration:0.3]; 
    [UIView setAnimationDelegate:self]; 
    [cellAnimationContainer setAlpha:0.4]; 
    [cellAnimationContainer setFrame:CGRectMake(150, 420, 20, 20)]; 
    [UIView commitAnimations]; 
} 
else if([anim isEqual:@"addFavorite-Step2"]) 
{ 
    [cellAnimationContainer removeFromSuperview]; 
    [cellAnimationContainer release];   
} 
} 
-1

Si esto es posible al igual que

-(void)click:(id)sender 
{ 
    int clickcelltag; 
    clickcelltag=sender.tag; 

    NSIndexPath *index =[NSIndexPath indexPathForRow:clickcelltag inSection:0]; 

    [userreviewtblview scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:YES];//userreviewtblview is a UITableView name 
} 

Hope esto le ayudará a :-)

Cuestiones relacionadas