从“visible”获取 UITableviewCell 位置区域或窗口

发布于 2024-11-05 10:02:58 字数 344 浏览 3 评论 0原文

我基于 uitableview 和 uitabbar 创建了一个 iPhone 应用程序。在表格视图的每个单元格上,我都有一个“添加到收藏夹按钮”。 当我按下此按钮时,我想让单元格从她的位置“跳转”到选项卡栏最喜欢的项目(与安装中的下载效果相同)

如果我位于前 10 个单元格,但如果我滚动,效果效果很好在tableView中效果不好,因为起始位置是根据uitableview大小计算的。

可以从可见区域知道单元格的位置。 ?

示例:对于第四十个单元格位置是 x:0,y:1600,w:320,y:50,我想要从屏幕顶部开始的位置而不是从 uiview 顶部开始的位置,所以像这样的 x:0,y :230,宽:320,纵:50

I create an iPhone app base on uitableview and uitabbar. On each cell of tableview i have an "add to favorite button".
When i pressed this button, i want to make the cell "jump" from her position to the favorite item of the tabbar (same as download effect in installous)

The effect work well if i'am on top 10 cell, but if i scroll in the tableView the effect is not good because start position is calculate from uitableview size.

It's possible to know the position of a cell from the visible area. ?

Exemple : for the fortieth cell position is x:0,y:1600,w:320,y:50, i want the position from the top of the screen not from the top of the uiview so something like this x:0,y:230,w:320,y:50

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮萍、无处依 2024-11-12 10:02:58

是的,您可以使用 rectForRowAtIndexPath: ,您只需传入您单击的行的索引路径即可。

rectForRowAtIndexPath:

返回一行的绘图区域
由索引路径标识。

...

编辑

转换:

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

Yes you can use rectForRowAtIndexPath: you'll just have to pass in the indexPath of your row you've clicked.

rectForRowAtIndexPath:

Returns the drawing area for a row
identified by index path.

...

EDIT

Conversion:

CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
CGRect rectInSuperview = [tableView convertRect:rectInTableView toView:[tableView superview]]; 
樱花坊 2024-11-12 10:02:58

迅速

let rectInTableView = tableView.rectForRow(at: indexPath)

let rectInSuperview = tableView.convert(rectInTableView, to: tableView.superview)

In swift

let rectInTableView = tableView.rectForRow(at: indexPath)

let rectInSuperview = tableView.convert(rectInTableView, to: tableView.superview)
私藏温柔 2024-11-12 10:02:58

所以这是我使用的代码:(我确信它可以优化):

首先将 NSindexPath ** 属性添加到您的自定义单元格。
*在您的 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];         
}
}

So here is the code i used : (it can be optimized i'm sure) :

First add NSindexPath ** property to your custom cell.
*Implement this in your tableview controller : *

-(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];         
}
}
情痴 2024-11-12 10:02:58

是的,这是可能的

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

希望这会帮助你:-)

Yes this is possible like that

-(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 this will help you :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文