Tej*_*udi 2 iphone gesture-recognition ios4
我正在尝试实现类似界面的Twitter iPhone应用程序.(滑动以使用自定义视图替换tableviewcell中的视图).我正在使用Apple UISwipeGestureRecognizer来识别滑动,我正在使用该滑动获取起始位置[recognizer locationInView:self.view].这给了我一个CGPoint,我正在使用它[tableView indexPathForRowAtPoint:location].我的问题是,我的滑动似乎总是被检测到在我刷过的实际行的上方或下方的一行.有没有人遇到过同样的问题?
编辑:我可能还应该提到我正在使用自定义tableview单元格,它的高度超过默认视图.我不确定这是否有任何区别,我正在使用它heightForRowIndexAtPath:来恢复高度.
我的代码是 -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ModelObject *rowData = (ModelObject *)[tempArr objectAtIndex:indexPath.row];
if (rowData.isAlternateView) {
// Load alternateview
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[cell addGestureRecognizer:recognizer];
[recognizer release];
return cell;
}
else {
// Load the correct uitableviewcell
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[cell addGestureRecognizer:recognizer];
[recognizer release];
return cell;
}
}
-(void) handleSwipe:(UISwipeGestureRecognizer *) recognizer
{
NSLog(@"Swipe detected!");
CGPoint location = [recognizer locationInView:self.view];
NSIndexPath *selectedIndexPath = [tableView indexPathForRowAtPoint:location];
NSLog(@"Swipe detected at %d", selectedIndexPath.row);
ModelObject *rowData = (ModelObject *)[modelArr objectAtIndex:selectedIndexPath.row];
rowData.isAlternateView = YES;
for (int i=0; i<[tempArr count]; i++) {
if (i!=selectedIndexPath.row) {
ModelObject *rowToBeCleared = (ModelObject *) [modelArr objectAtIndex:i];
if ([rowToBeCleared isAlternateView]) {
[rowToBeCleared setIsAlternateView:NO];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:i inSection:0],nil] withRowAnimation:UITableViewRowAnimationLeft];
}
}
}
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:selectedIndexPath,nil] withRowAnimation:UITableViewRowAnimationRight];
}
Run Code Online (Sandbox Code Playgroud)
小智 10
首先,我建议不要在每个单元格中添加一个手势识别器,而是在包含表格视图的整个视图中添加一个手势识别器(或者如果这是一个UITableViewController,那么它们就是同一个东西).
在viewDidLoad中:
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];
Run Code Online (Sandbox Code Playgroud)
然后,在handleSwipe方法中:
- (void)handleSwipe:(UISwipeGestureRecognizer *) recognizer
{
//might need to check if swipe has ended (otherwise not time to handle yet)
if (recognizer.state != UIGestureRecognizerStateEnded)
return;
CGPoint location = [recognizer locationInView:tableView]; //not self.view
NSIndexPath *selectedIndexPath = [tableView indexPathForRowAtPoint:location];
if (selectedIndexPath != nil)
{
//user swiped on a tableview cell
}
else
{
//user did not swipe on a tableview cell
}
}
Run Code Online (Sandbox Code Playgroud)