【问题标题】:UITapGestureRecognizer inexplicably stops recognizing tap gesturesUITapGestureRecognizer 莫名其妙地停止识别点击手势
【发布时间】:2014-01-08 06:09:21
【问题描述】:

尽可能简单地说,我分配给UIImageViewUITapGestureRecognizer 将不可避免地莫名其妙地停止触发它所指向的方法。

它甚至更奇怪,因为有时它仅在一次点击后就无法识别手势,而其他时候则需要数十次点击。但是,每次都不会失败,它不可避免地会停止。

我尝试将点击手势设置为强关联属性,但没有效果。

我最近尝试过的(没有成功)是,在手势的选择器方法运行正常后,我删除了手势,然后重新分配并重新初始化了一个新的UITapGestureRecognizer,但没有任何效果。这让我相信问题出在UIImageView 而不是UITapGuestureRecognizer - 但话虽如此,我不知道。

但是,我将 UIImageView 放入了几个 UIView 动画中,所以也许这与它有关?

另外,UIImageView 已启用用户交互,我从未禁用它。

有什么建议吗?如果有帮助,我很乐意发布代码。这是一些代码:

设置 UIImageView(图像视图和点击手势都已设置为属性,以便我可以将它们强烈关联):

self.cardImageView = [[UIImageView alloc] initWithFrame:frame];
[self.cardImageView setContentMode:UIViewContentModeScaleAspectFill];
[self.cardImageView setClipsToBounds:TRUE];
[self.cardImageView setBackgroundColor:[UIColor nearBlack]];
[self.cardImageView.layer setBorderColor:[[UIColor fiftyGray]CGColor]];
[self.cardImageView.layer setBorderWidth:1.0];
[self.cardImageView setUserInteractionEnabled:TRUE];

self.imageFullScreenTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView)];
[self.cardImageView addGestureRecognizer:self.imageFullScreenTap];

[view addSubview:self.cardImageView];

动画:

[UIView animateWithDuration:0.2 animations:^
     {
         [self.cardImageView setFrame:[self frameForImageView]];

         [page setAlpha:!fullscreenTemplate];
         [saveExitButton setAlpha:!fullscreenTemplate];
         [optionsButton setAlpha:!fullscreenTemplate];

         if(fullscreenTemplate)
         {
             [self.cardImageView.layer setBorderColor:[UIColor clearColor].CGColor];
             [self.view setBackgroundColor:[UIColor blackColor]];
         }
         else
         {
             [self.cardImageView.layer setBorderColor:[UIColor fiftyGray].CGColor];
             [self.view setBackgroundColor:[UIColor clearColor]];
         }
     }
     completion:^(BOOL finished)
     {
         [scroller setScrollEnabled:!fullscreenTemplate];

         if (self.imageFullScreenTap)
         {
             [self.cardImageView removeGestureRecognizer:self.imageFullScreenTap];
             self.imageFullScreenTap = nil;
         }

         self.imageFullScreenTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView)];
         [self.cardImageView addGestureRecognizer:self.imageFullScreenTap];
     }];

【问题讨论】:

  • 您添加“少数 UIView 动画”的子视图可能有问题,因此每次您必须对所有 uiview(s)=YES 进行用户操作;
  • 如果您能与我们分享一些代码会很有帮助,例如gestureRecogniser 的分配、imageView 的分配以及UIViewAnimation 块。
  • 你的 UIImageView 是你添加手势识别器的属性吗?因为它应该是。
  • @JohnWoods 是的,我已经在上面发布了代码
  • @n00bProgrammer 已发布。还有什么?

标签: iphone objective-c ios7 uitapgesturerecognizer


【解决方案1】:
[UIView transitionWithView:self.cardImageView
                  duration:0.2f 
                   options:UIViewAnimationOptionAllowUserInteraction |
                        UIViewAnimationOptionLayoutSubviews
                animations:^(void) {
                    [self.cardImageView setFrame:[self frameForImageView]];
                    [page setAlpha:!fullscreenTemplate];
                    [saveExitButton setAlpha:!fullscreenTemplate];
                    [optionsButton setAlpha:!fullscreenTemplate];
                    if(fullscreenTemplate) {
                        [self.cardImageView.layer setBorderColor:[UIColor clearColor].CGColor];
                        [self.view setBackgroundColor:[UIColor blackColor]];
                    } else {
                        [self.cardImageView.layer setBorderColor:[UIColor fiftyGray].CGColor];
                        [self.view setBackgroundColor:[UIColor clearColor]];
                    }
                } completion:^(BOOL finished) {
                    [scroller setScrollEnabled:!fullscreenTemplate];
                }]; 

以上代码已将animateWithDuration:completion: 方法更改为transitionWithView:duration:options:animations:completion: 方法。 这里重要的关键字是UIViewAnimationOptionAllowUserInteraction。这将允许在图像制作动画时进行 userInteraction。

如果一段时间后 TapGesture 仍然无法识别,请告诉我你的 tapImageView 方法的代码。

【讨论】:

  • 你可能已经解决了!这个问题一直在杀死我。谢谢 - 多一点测试,如果它坚如磐石,赏金就是你的
  • 拍,又出事了
  • 您能在 GitHub 上上传您项目的简化版本吗?你在任何地方做gestureReconnizer.enable = NO 吗?
  • 我宁愿不上传项目,但要回答你的问题,我不这样做。我可以修改问题以显示更多代码,尽管@Warif Akhand Rishi
  • 很可能你会得到赏金,因为这个问题不是热门话题。在我解决这个问题时,您介意继续提供支持吗?
【解决方案2】:

使用UIViewAnimationOptionAllowUserInteractionbringSubviewToFront

[view addSubview:self.cardImageView];
[view bringSubviewToFront:self.cardImageView];

动画:

[UIView animateWithDuration:0.2
                          delay:0.0         
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:^
                             {
                                [self.cardImageView setFrame:[self frameForImageView]];

                                [page setAlpha:!fullscreenTemplate];
                                [saveExitButton setAlpha:!fullscreenTemplate];
                                [optionsButton setAlpha:!fullscreenTemplate];

                                if(fullscreenTemplate)
                                {
                                  [self.cardImageView.layer setBorderColor:[UIColor clearColor].CGColor];
                                  [self.view setBackgroundColor:[UIColor blackColor]];
                                }
                                else
                                {
                                  [self.cardImageView.layer setBorderColor:[UIColor fiftyGray].CGColor];
                                  [self.view setBackgroundColor:[UIColor clearColor]];
                                }
                             }
                             completion:^(BOOL finished)
                             {
                               [scroller setScrollEnabled:!fullscreenTemplate];

                               if (self.imageFullScreenTap)
                               {
                                 [self.cardImageView removeGestureRecognizer:self.imageFullScreenTap];
                                 self.imageFullScreenTap = nil;
                                } 

                                self.imageFullScreenTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView)];
                                [self.cardImageView addGestureRecognizer:self.imageFullScreenTap];
                             }];  

【讨论】:

    【解决方案3】:

    如果您在某个时候将图像视图添加到 uiscrollview,那么恐怕大多数时候手势识别器都无法工作。或者你尝试在父视图上启用用户交互。

    【讨论】:

    • 图像视图在 -(void)loadView{} 的方法覆盖中添加到滚动视图中。您是否认为它仍然可能需要您的解决方案?
    • 有什么可以试一试的?
    • 好点,阿努布拉塔。我会试一试,然后回复你
    • 对不起...我会尝试找出新的方法
    猜你喜欢
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多