UIView added as an UIWindow subview doesn't respond to taps

后端 未结 3 708
梦如初夏
梦如初夏 2021-01-18 23:03

I\'ve added a UIView containing a UITapGestureRecognizer as my key window\'s subview. It shows properly, however when I tap my view, the target met

3条回答
  •  既然无缘
    2021-01-18 23:48

    Updated NotificationView.m file, as suggested by @DBD...

    #import "NotificationView.h"
    
    @implementation NotificationView
    
    - (id)initWithMessage:(NSString*)message andColor:(UIColor*)color andKind:(NotificationKind)kind
    {
        self = [super initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 60)];
        if (self) {
            // Initialization code
            [self setAlpha:0];
            [self setBackgroundColor:color];
            [self setClipsToBounds:YES];
    
            currentNotificationKind = kind;
    
            UILabel *label = [[UILabel alloc] initWithFrame:self.bounds];
            [label setNumberOfLines:0];
            [label setFont:[UIFont fontWithName:@"Roboto-Italic" size:20]];
            [label setTextColor:[UIColor whiteColor]];
            [label setTextAlignment:NSTextAlignmentCenter];
            [label setPreferredMaxLayoutWidth:290];
            [label setText:message];
            [self addSubview:label];
    
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(notificationTapped)];
            [tap setNumberOfTapsRequired:1];
            [self addGestureRecognizer:tap];
    
            [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    
        }
        return self;
    }
    
    -(void)show{
    
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    
            [self setAlpha:1];
    
        } completion:nil];
    
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    
            [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
                [self setAlpha:0];
    
            } completion:^(BOOL finished) {
                [self removeFromSuperview];
            }];
    
        });
    
    
    }
    
    -(void)notificationTapped{
    
        DDLogDebug(@"Notification tapped!");
    
    }
    
    @end
    

提交回复
热议问题