Get UITableviewCell position from “visible” area o

2019-02-02 04:53发布

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

3条回答
Viruses.
2楼-- · 2019-02-02 05:29

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 :-)

查看更多
Deceive 欺骗
3楼-- · 2019-02-02 05:33

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];         
}
}
查看更多
做自己的国王
4楼-- · 2019-02-02 05:40

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]]; 
查看更多
登录 后发表回答