UITapGestureRecognizer没有在UIView上工作

时间:2014-05-01 07:47:09

标签: ios objective-c uiview uitapgesturerecognizer

我已经在SO上阅读了多个解决方案,但它们似乎都不适用于我的情况。

我有一个NotificationBar类: .h文件:

@interface NotificationBar : UIView <UIGestureRecognizerDelegate>

@property (strong, nonatomic) UILabel *messageLabel;

- (id)initWithFrame:(CGRect)frame message:(NSString *)message;

- (void) show:(int)duration;

@end

.m文件:

#import "NotificationBar.h"

@implementation NotificationBar

@synthesize messageLabel;

- (id)initWithFrame:(CGRect)frame message:(NSString *)message
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.alpha = 0;
        self.backgroundColor = [UIColor cabmanBlue];
        self.userInteractionEnabled = YES;

        self.messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.origin.x + 5, 0 , self.bounds.size.width - 10, self.bounds.size.height)];
        [self.messageLabel setBackgroundColor:[UIColor clearColor]];
        [self.messageLabel setTextColor:[UIColor whiteColor]];
        [self.messageLabel setFont:[UIFont boldSystemFontOfSize:14]];
        [self.messageLabel setNumberOfLines:2];
        self.messageLabel.text = message;
        [self addSubview:messageLabel];

        UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
        tapRec.delegate = self;
        tapRec.cancelsTouchesInView = NO;
        [self addGestureRecognizer:tapRec];
    }
    return self;
}

- (void) show:(int)duration
{
    [[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject] addSubview:self];
    [UIView animateWithDuration:0.5 animations:^{ self.alpha = 1; } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 delay:duration options:UIViewAnimationOptionCurveLinear animations:^{ self.alpha = 0;} completion:^(BOOL finished)
         {
             if(finished) [self removeFromSuperview];
         }];
    }];
}

- (void) dismiss:(UITapGestureRecognizer *)gesture
{
    [self.layer removeAllAnimations];
    [UIView animateWithDuration:0.5 animations:^{ self.alpha = 0; } completion:^(BOOL finished) {
        if(finished)
        {
            [self removeFromSuperview];
        }
    }];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

@end

我以下列方式使用该课程:

- (void) displayNotificationMessage:(NSString *)message withSound:(BOOL) withSound
{
    UIView *topView = [self.window.subviews lastObject];
    [[[NotificationBar alloc] initWithFrame:CGRectMake(topView.bounds.origin.x,
                                                                             64,
                                                                             topView.bounds.size.width,
                                                                             40)
                            message:message] show:10];

    if(withSound) AudioServicesPlaySystemSound(1007);
}

始终显示和显示视图。解雇功能没有被触发,似乎它没有响应任何事情。 displayNotificationMessage放在AppDelegate中。 displayNotificationMessage有时会在显示带有mapview的viewcontroller时或在UITableViewController中使用。您可以说它必须以与UIAlertView相同的方式工作:无论用户在哪个屏幕上,都会始终显示。

有没有人看到错误或其他什么? 提前谢谢!

2 个答案:

答案 0 :(得分:0)

你的UILabel几乎占据了你的整个视角:

self.messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.origin.x + 5, 0 , self.bounds.size.width - 10, self.bounds.size.height)];

您可以尝试缩小它的大小,然后查看手势是否在其他位置有效,或者您可以尝试将手势添加到标签

[self.messageLabel addGestureRecognizer:singleTap];  

答案 1 :(得分:0)

我刚刚在自定义UIView上尝试了你的代码,它运行得很好。将UILabel作为子视图没有效果,它反应良好。对我来说,看起来你可能有另一个UIView放在这个(这使得这个被埋没,因此没有反应)或者你在superview注册了另一个Tap Gesture。