UIView ajouté comme une sous-vue UIWindow ne répond pas aux robinets

J'ai ajouté un UIView contenant un UITapGestureRecognizer comme sous- UITapGestureRecognizer de ma window key. Il s'affiche correctement, mais lorsque j'appuie sur ma vue, la méthode cible n'est pas déclenchée. J'ai même essayé de replace le reconnaisseur de geste avec un UIButton , toujours en vain.

Voici mon code.

NotificationView.h

 #import <UIKit/UIKit.h> typedef NS_ENUM(int, NotificationKind) { NotificationKindActivity, NotificationKindReply, }; @interface NotificationView : UIView { NotificationKind currentNotificationKind; } -(id)initWithMessage:(NSSsortingng*)message andColor:(UIColor*)color andKind:(NotificationKind)kind; -(void)show; @end 

NotificationView.m

 #import "NotificationView.h" @implementation NotificationView - (id)initWithMessage:(NSSsortingng*)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 setUserInteractionEnabled: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]; [label setUserInteractionEnabled:YES]; [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 animations:^{ [self setAlpha:1]; } completion:^(BOOL finished) { [UIView animateWithDuration:0.3 delay:3 options:UIViewAnimationOptionCurveLinear animations:^{ [self setAlpha:0]; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; }]; } -(void)notificationTapped{ DDLogDebug(@"Notification tapped!"); } @end 

Quand cela m'arrive, c'est généralement parce que j'ai UIView mon cadre UIView . Je vois tout le contenu comme prévu car mon UIView n'est pas UIView aux limites, mais je ne peux pas interagir avec quoi que ce soit parce que mes taps sont en dehors des limites de l' UIView .

Mon test simple est de changer la couleur d'arrière-plan de l' UIView et de voir si elle couvre la zone que j'attends ou si je bloque la taille / le placement d'une manière ou d'une autre.

J'avais l'habitude de battre ma tête contre le mur avec ce problème, luttant pendant des heures, mais je l'ai fait tellement de fois maintenant c'est 5 minutes de "Oh ça encore".

Modifier:

Ensuite, je regarderais votre code de show . Votre code d'appel n'est pas là, mais si je suppose que vous utilisez votre code d'access et que votre vue n'est affichée que pendant 3 secondes, alors c'est votre problème.

Comme Justin l'a mentionné (commentaire ci-dessus) et les docs d'Apple

Pendant une animation, les interactions user sont temporairement désactivées pour toutes les vues impliquées dans l'animation, quelle que soit la valeur de cette propriété. Vous pouvez désactiver ce comportement en spécifiant l'option UIViewAnimationOptionAllowUserInteraction lors de la configuration de l'animation.

Puisque tout le time que votre vue est à l'écran fait partie d'un bloc d'animation, toute interaction sera désactivée pendant tout le time où elle est visible. Je n'ai jamais vraiment testé le bit de delay et si l'animation a été désactivée pendant ce morceau, mais cela ne m'étonnerait pas que l'animation soit désactivée pendant le timeout. La deuxième animation se trouve toujours dans le bloc d'animation principal, donc je suppose que les animations seront bloquées jusqu'à ce que les deux soient complètes.

Fichier NotificationView.m mis à jour, tel que suggéré par @DBD …

 #import "NotificationView.h" @implementation NotificationView - (id)initWithMessage:(NSSsortingng*)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