UITapGestureRecognizer 点击效果出现两次或者多次的问题

本文探讨了在iOS开发中,如何解决快速连续点击导致的手势识别重复问题。通过调整动画时长和手势识别器属性,避免了多次触发同一操作的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(allViewAction:)];

         tap.cancelsTouchesInView = YES;

        tap.numberOfTapsRequired = 1;

        [self.bubleImageView addGestureRecognizer:tap];

        [tap release];

如上面代码,为一个imageview增加一个点击手势,在点击很快的时候,会发现,allviewAction方法会执行多次。而导致

FtPreviewFileViewController  *previewFileViewController = [[FtPreviewFileViewController alloc] initWithFileContent:content previewType:FTFilePreviewTypeChat];

                [GetIFtStatisticsPoint addActionTimesWithId:SP_FILE_RECEIVEFILE_DOWNLOADSUC_OPENFILE];

        

        FtRootViewController* vc = [[FtAppDelegate shareAppDelegate] rootViewCtrl];

        

        UIView* curView = nil;

        [previewFileViewController.view setFrame:CGRectMake(78, IOS7_MARGIN, 946, 748)];

        [previewFileViewController setViewFrame];

        [vc.view addSubview:previewFileViewController.view];

        curView = previewFileViewController.view;

        curView.frame = CGRectMake(946, IOS7_MARGIN, 946, 748);

        [UIView animateWithDuration:1.5f animations:^{

            curView.frame = CGRectMake(78,IOS7_MARGIN, 946, 748);

        } completion:^(BOOL finished) {

            

        }];

这个代码段也会执行多次。这是为何呢?

原来cancelsTouchesInView为YES,表示当Gesture Recognizers识别到手势后,会向hit-test view发送 touchesCancelled:withEvent:消息来取消hit-test view对此触摸序列的处理,这样只有Gesture Recognizers能响应此触摸序列,hit-test view不再响应。如果为NO,则不发送touchesCancelled:withEvent:消息给hit-test view,这样会使Gesture Recognizers和hit-test view同时响应触摸序列。

就是说cancelsTouchesInView为YES后,并且这个点击效果已经产生 了,才不会继续接受新的点击响应,否则系统会认为没有收到点击效果,会继续接受点击效果继续响应点击方法。


所以我们将点击的响应展现新界面的方法里面的动画时间改短一些,让界面及时的弹出来。在多次点击视图的时候,就不会多次接收点击了。因为界面弹出后cancelsTouchesInView会立刻起作用,不再接受新的点击效果。


于是将

 [UIView animateWithDuration:1.5f animations:^{

            curView.frame = CGRectMake(78,IOS7_MARGIN, 946, 748);

        } completion:^(BOOL finished) {

            

        }];

改成

 [UIView animateWithDuration:0.01f animations:^{

            curView.frame = CGRectMake(78,IOS7_MARGIN, 946, 748);

        } completion:^(BOOL finished) {

            

        }];

在多次点击时,就不会弹出多个界面了。


转载于:https://my.oschina.net/u/2252309/blog/389106

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值