我正在尝试检测左侧滑动,因此在开始时添加到我的tableview:
//swipe left
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(leftSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.tableView addGestureRecognizer:recognizer];
然后用以下方法检测细胞本身:
- (void)leftSwipe:(UISwipeGestureRecognizer *)gestureRecognizer
{
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
//show some menu in the cell position( its cell.frame)
}
仅当桌面视图没有向下滚动时才有效,这意味着在我向下滚动后,我检测到的检测位置是错误的,因此如果我尝试添加一些侧面菜单,它的位置不正确。
如果我在开始时检查这一点,而不滚动,则效果很好。
为什么?
答案 0 :(得分:0)
解决。当我添加视图时,我将它放在视图中,而不是在tableView中,因此位置错误,因为它们是相对于滚动条计算的,而不是它自己的视图。
将菜单添加到tableView,将其放在正确的位置。
答案 1 :(得分:0)
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"LazyTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor=[UIColor clearColor];
if (tableView == tableviewMsg)
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
for (UIView *view in cell.contentView.subviews)
[view removeFromSuperview];
}
UIView *SwipeView = [[UIView alloc]init];
SwipeView.frame = CGRectMake(0,0,394,50);
SwipeView.tag = -1001;
[SwipeView setBackgroundColor:[UIColor clearColor]];
[SwipeView setUserInteractionEnabled:YES];
[cell.contentView addSubview:SwipeView];
if([indexPath isEqual:indexSelected])
{
CGRect rect = SwipeView.frame;
rect.origin.x = -45;
SwipeView.frame = rect;
}
UISwipeGestureRecognizer* swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedLeft:)];
[swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[cell addGestureRecognizer:swipeGestureLeft];
UISwipeGestureRecognizer* swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedRight:)];
[swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:swipeGestureRight];
cell.backgroundColor=[UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)cellSwipedLeft:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
NSIndexPath* indexPath = [tableviewMsg indexPathForCell:cell];
if ([indexPath isEqual:indexSelected]) {
}
else{
[self hideButtonForIndex:indexSelected animated:YES];
[self showButtonForIndex:indexPath animated:YES];
}
}
}
- (void)cellSwipedRight:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
NSIndexPath* indexPath = [tableviewMsg indexPathForCell:cell];
if ([indexPath isEqual:indexSelected])
{
[self hideButtonForIndex:indexSelected animated:YES];
}
}
}