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

时间:2014-01-08 06:09:21

标签: iphone objective-c ios7 uitapgesturerecognizer

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

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

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

我最近尝试过的(没有成功)是,在手势的选择器方法运行之后,我删除了手势,然后重新分配并重新初始化了一个新的UIImageView并且没有影响。这让我相信问题就在于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];

3 个答案:

答案 0 :(得分: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:方法。 Importent keyWord这里是UIViewAnimationOptionAllowUserInteraction。这将允许在图像动画时进行userInteraction。

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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