UIView added as UIWindow subtitle does not respond to taps

I added a UIView containing a UITapGestureRecognizer as a subcategory of my key window. It displays correctly, however, when I click on my image, the target method does not start. I even tried to replace the gesture recognizer with UIButton , but to no avail.

Here is my code.

NotificationView.h

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

NotificationView.m

 #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 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 
+5
source share
3 answers

When this happens to me, this is usually because I screwed the UIView frame. I see all the contents as expected because my UIView not clipped to the borders, but I cannot interact with anything because my branches are outside of the UIView .

My simple test is to change the background color of the UIView and see if it covers the area that I expect, or if I messed up the size / place somehow.

I used to face this problem struggling for hours, but I did it many times, now it’s 5 minutes to fix β€œOh, this again.”

Edit:

Then I will look at your show code. Your calling code is missing here, but if I assume that you just use your show code and your view is only shown on the screen for 3 seconds, then this is the problem.

As Justin mentioned (comment above) and Apple docs

During the animation, user interactions are temporarily disabled for all views participating in the animation, regardless of the value in this property. You can disable this behavior by specifying the UIViewAnimationOptionAllowUserInteraction Option when setting up the animation.

Since all the time your presentation on the screen is part of the animation block, all interactions will be turned off as long as they are visible. I never tested the delay bit and whether the animation was turned off during this part, but it would not be surprising that the animation was turned off during the delay. The second animation is still inside the main animation block, so I assume that the animation will be locked until both are complete.

+12
source

NotificationView.m file updated 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 
0
source

I also could not get gestures to work in subviews, which I added directly to [UIApplication sharedApplication] .keyWindow. (This is the standard way to create views that take up the whole screen behind Add UIView, above all, even the navigation bar .)

The only solution I could find was to add a subview instead of [UIApplication sharedApplication] .windows [0] and hide the status bar with a little help. The status bar will not disappear .

If anyone knows how to make gestures work in subviews added to keyWindow, let me know!

0
source

Source: https://habr.com/ru/post/1200599/


All Articles