Получить позицию UITableviewCell из "видимой" области или окна
Я создаю базу приложений iPhone на uitableview и uitabbar. В каждой ячейке tableview у меня есть кнопка "добавить в избранное".
Когда я нажал эту кнопку, я хочу, чтобы ячейка "перескочила" с ее позиции на любимый элемент на вкладке (так же, как и эффект загрузки в installous).
Эффект хорошо работает, если я на верхней 10 ячейке, но если я прокручу в таблице, эффект не будет хорош, потому что начальная позиция вычисляется из размера uitableview.
Возможно узнать положение ячейки из видимой области.
Пример: для сороковой ячейки позиция x: 0, y: 1600, w: 320, y: 50, я хочу, чтобы позиция сверху экрана не была сверху uiview, поэтому что-то вроде этого x: 0, у: 230, вес: 320, у: 50
Ответы
Ответ 1
Да, вы можете использовать rectForRowAtIndexPath:
, вам просто нужно будет пройти в indexPath вашей строки, которую вы нажали.
rectForRowAtIndexPath:
Возвращает область рисования для строки идентифицированный указательным путем.
...
ИЗМЕНИТЬ
Преобразование:
CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
CGRect rectInSuperview = [tableView convertRect:rectInTableView toView:[tableView superview]];
Ответ 2
Итак, вот код, который я использовал: (его можно оптимизировать, я уверен):
Сначала добавьте свойство 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];
}
}
Ответ 3
Да, это возможно так.
-(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
}
Надеюсь, это поможет вам: -)