我创建了一个基于uitableview和uitabbar的iPhone应用程序。在表格视图的每个单元格上,我都有一个"添加到收藏夹按钮"。当我按下这个按钮时,我想让单元格从她的位置"跳"到标签栏的收藏项目(与installous中的下载效果相同)
如果我在前 10 个单元格上,效果效果很好,但是如果我在 tableView 中滚动,效果不好,因为起始位置是根据 uitableview 大小计算的。
可以从可见区域知道单元格的位置。
示例:对于第四十个单元格位置是 x:0,y:1600,w:320,y:50,我希望屏幕顶部的位置而不是 uiview 顶部的位置,所以像这样 x:0,y:230,w:320,y:50
是的,您可以使用rectForRowAtIndexPath:
您只需传入您单击的行的 indexPath。
rectForRowAtIndexPath:
返回行的绘图区域 由索引路径标识。
。
编辑
转换:
CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
CGRect rectInSuperview = [tableView convertRect:rectInTableView toView:[tableView superview]];
所以这是我使用的代码: (我敢肯定可以优化):
首先将 NSindexPath ** 属性添加到自定义单元格。
*在您的表视图控制器中实现这一点:*
-(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];
}
}
是的,这是可能的
-(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
}
希望这会对您有所帮助:-)