开发者

Get UITableviewCell position from "visible" area or window

开发者 https://www.devze.com 2023-03-03 13:15 出处:网络
I create an iPhone app base on uitableview and uitabbar. On each cell of tableview i have an \"add to favorite button\".

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


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]]; 


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消