首页 文章

UITapGestureRecognizer莫名其妙地停止识别轻击手势

提问于
浏览
6

尽可能简单地说,我已经分配给 UIImageViewUITapGestureRecognizer 将不可避免地并且莫名其妙地停止触发它所针对的方法 .

它甚至更奇怪,因为有时它只会在一次敲击后停止识别手势,而其他时间则需要数十次敲击才能识别 . 但是,每次都没有失败,它将不可避免地停止 .

我已经尝试将轻拍手势设置为强关联属性,但这没有任何效果 .

我的选择器方法已经运行了,我删除了手势然后重新分配并重新初始化了一个新的 UITapGestureRecognizer 并且没有效果 . 这让我相信问题就在于 UIImageView 而不是 UITapGuestureRecognizer - 但话虽如此,我不知道 .

但是,我正在通过一些 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];
     }];

3 回答

  • 0
    [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];
                    }];
    

    上面的代码用 transitionWithView:duration:options:animations:completion: 方法更改了 animateWithDuration:completion: 方法 . Importent keyWord这里是 UIViewAnimationOptionAllowUserInteraction . 这将允许在图像动画时进行userInteraction .

    如果TapGesture在一段时间后仍然停止识别,请向我显示 tapImageView 方法的代码 .

  • 1

    使用 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];
                                 }];
    
  • 0

    如果你在某个时候将imageview添加到uiscrollview,那么我担心手势识别器大多数时候都不会工作 . 或者您尝试在父视图上启用userinteraction .

相关问题