• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - uitableview 单元格中的手势错误

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:38:21 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我的 UITableViewCell 中的 GestureRecognizer 有问题,我有左右滑动手势,长按 2 秒并在表格 View 单元格中捏手势,滑动- 向右滑动下一章,向左滑动查看上一章,捏住重定向到另一个 View Controller ,按住 2 秒以显示弹出窗口( subview )。所有这些都在一个名为 table 的 tableview 中实现。我的问题是一切正常,但是在 tableview 中使用 Gesture 一段时间后,它卡住了 swipeGesture 可能会捏也不起作用。但是当我转到另一个页面并回来时,它开始正常工作。一段时间后它也会卡住。我的代码是如何解决这个问题的?

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {

   static NSString *CellIdentifier = @"Cell";

    readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self actionselector(handleSwipeGesture];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; // or whatever
    [table addGestureRecognizer:swipeGesture];
    [swipeGesture release];

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
                                          initWithTarget:self actionselector(handleLongPress];
    lpgr.minimumPressDuration = 1.0; //seconds
    lpgr.delegate = self;
    [table addGestureRecognizer:lpgr];
    [lpgr release];

    UISwipeGestureRecognizer *swipeGestureleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self actionselector(handleSwipeGestureleft];
    swipeGestureleft.direction = UISwipeGestureRecognizerDirectionLeft; // or whatever
    [table addGestureRecognizer:swipeGestureleft];
    [swipeGestureleft release];

    UIPinchGestureRecognizer *longPressRecognizer = 
    [[UIPinchGestureRecognizer alloc]
     initWithTarget:self 
     actionselector(longPressDetected];
    [self.view addGestureRecognizer:longPressRecognizer];
    [longPressRecognizer release];

-(void)handleLongPressUILongPressGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:table];

    NSIndexPath *indexPath = [table indexPathForRowAtPoint:p];
    if (indexPath == nil)
        NSLog(@"long press on table view but not on a row");
    else

        // localStringValueverseno= [tableView cellForRowAtIndexPath:indexPath];
        localStringValueverseno = [(UILabel *)[[table cellForRowAtIndexPath:indexPath]
                                               viewWithTag:1] text];
    localStringValue = [table cellForRowAtIndexPath:indexPath].textLabel.text; 
    _lblmainnotegns.text = localStringValueverseno;
    // localStringValueverseno= [tableView cellForRowAtIndexPath:indexPath].cell.table.chapterAndVerse.text; 
    delegate.selectedIndex=[NSString stringWithFormat"%d",indexPath.row+1];
    [userInfo setObject:[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row] forKey"text"];
    [userInfo setObject:delegate.selectedBook forKey"book"];
    [userInfo setObject:delegate.selectedChapter forKey"chapter"];
    [userInfo setObject:[NSString stringWithFormat"%d",indexPath.row + 1] forKey"verse"];

    UIView *noteView = (UIView *)[self.view viewWithTag:101]; 
    //ViewWithTag Number should be same as used while allocating
    [noteView removeFromSuperview];
    //For removing View
    UIView *acntView = (UIView *)[self.view viewWithTag:100]; 
    //ViewWithTag Number should be same as used while allocating
    [acntView removeFromSuperview];
    // now you can use cell.textLabel.text
    UIView *noteViewsync = (UIView *)[self.view viewWithTag:105]; 
    //ViewWithTag Number should be same as used while allocating
    [noteViewsync removeFromSuperview];


    if ([notes objectForKey:[NSString stringWithFormat:@"%@ %@:%@",delegate.selectedBook,delegate.selectedChapter,delegate.selectedIndex]] == nil) {
        [notetickimage setHidden:YES];
        [self.view addSubview:MainPopupView];
    }
    if (gestureRecognizer.state==UIGestureRecognizerStateBegan ) {
        self.table.scrollEnabled = NO;
        [table removeGestureRecognizer:gestureRecognizer];
    }
    else if(gestureRecognizer.state==UIGestureRecognizerStateEnded) {
        self.table.scrollEnabled = YES;
        [table removeGestureRecognizer:gestureRecognizer];
    }



        NSLog(@"long press on table view at row %d", indexPath.row);
}
-(void) handleSwipeGestureUISwipeGestureRecognizer*)recognizer {


     if(![delegate.selectedChapter isEqualToString:[NSString stringWithFormat:@"%d",[DbHandler mNumberOfChaptersInBook:delegate.selectedBook]]]) {
         [UIView beginAnimations:nil context:nil];
         [UIView setAnimationDuration:0.4];
         [UIView setAnimationDelay:0.0];
         [UIView setAnimationBeginsFromCurrentState:YES];
         [UIView setAnimationDidStopSelector:@selector(animCompleteHandler:finished:context];
         [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
         [UIView commitAnimations];

        // if the currentChapter is the last then do nothing
         delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] + 1];
         [delegate reloadVerses];
         [self resetReadViewToVerse:1];
         [table removeGestureRecognizer:recognizer];
    }
    if (recognizer.state==UIGestureRecognizerStateBegan ) {
        self.table.scrollEnabled = NO;
        [table removeGestureRecognizer:recognizer];
    }
    else if(recognizer.state==UIGestureRecognizerStateEnded) {
        self.table.scrollEnabled = YES;
        [table removeGestureRecognizer:recognizer];
    }


    return;



}
-(void) handleSwipeGestureleftUISwipeGestureRecognizer*)recognizer {

    if(![delegate.selectedChapter isEqualToString:@"1"])
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.4];
        [UIView setAnimationDelay:0.0];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDidStopSelector:@selector(animCompleteHandler:finished:context];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
        [UIView commitAnimations];
        delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] - 1];
        [delegate reloadVerses];
        [self resetReadViewToVerse:1]; 
        [table removeGestureRecognizer:recognizer];
    }
    if (recognizer.state==UIGestureRecognizerStateBegan ) {
        self.table.scrollEnabled = NO;
        [table removeGestureRecognizer:recognizer];
    }
    else if(recognizer.state==UIGestureRecognizerStateEnded) {
        self.table.scrollEnabled = YES;
        [table removeGestureRecognizer:recognizer];
    }

    return;


}
-(void) longPressDetectedUIPinchGestureRecognizer*)recognizer {


    self.table.scrollEnabled = NO;
    SearchViewController *aSecondViewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil];
    aSecondViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:aSecondViewController animated:YES];

    /*[self.navigationController pushViewController:aSecondViewController animated:YES];*/

    [aSecondViewController release];
    [UIView commitAnimations];
    [table removeGestureRecognizer:recognizer];

    UIView *noteView = (UIView *)[self.view viewWithTag:101]; 
    //ViewWithTag Number should be same as used while allocating
    [noteView removeFromSuperview];
    UIView *setngView = (UIView *)[self.view viewWithTag:102]; 
    //ViewWithTag Number should be same as used while allocating
    [setngView removeFromSuperview];
    UIView *acntView = (UIView *)[self.view viewWithTag:100]; 
    //ViewWithTag Number should be same as used while allocating
    [acntView removeFromSuperview];
    UIView *popView = (UIView *)[self.view viewWithTag:106]; 
    //ViewWithTag Number should be same as used while allocating
    [popView removeFromSuperview];

}

当我们向左或向右滑动时, Action 也很慢。 请帮帮我。



Best Answer-推荐答案


每次调用例程 tableView:cellForRowAtIndexPath: 时添加一个识别器,并在处理完手势后删除一个。因此,在移除所有识别器(它们的数量等于单元格的数量)后,将无法识别手势。

也许您不应该在处理例程中删除识别器?

关于iphone - uitableview 单元格中的手势错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8351772/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap