问题描述
我添加了一个 UIView
,其中包含 UITapGestureRecognizer
作为我的关键窗口的子视图。它显示正确,但是当我点击我的视图时,不会触发目标方法。我甚至试图用 UIButton
替换手势识别器,但仍无济于事。
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 method is not fired. I've even tried to replace the gesture recognizer with a UIButton
, still to no avail.
这是我的代码。
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
推荐答案
当我遇到这种情况时,通常是因为我搞砸了我的 UIView
框架。我看到所有内容都是预期的,因为我的 UIView
没有剪切到边界,但我无法与任何东西交互,因为我的水龙头超出了<$ c的界限$ c> UIView 。
When this happens to me it's usually because I screwed up my UIView
frame. I see all the content as expected because my UIView
isn't clipping to bounds, but I can't interact with anything because my taps are outside the bounds of the UIView
.
我的简单测试是更改 UIView $ c $的背景颜色c>看看它是否覆盖了我所期望的区域,或者我是否以某种方式搞砸了尺寸/位置。
My simple test is to change the background color of the UIView
and see if it covers the area I expect or if I screwed up size/placement somehow.
我常常用这个问题砸到墙上,挣扎着几个小时,但我已经这么做了很多次现在是5分钟修复哦再说。
I used to pound my head against the wall with this issue, struggling for hours, but I've done it so many times now it's 5min fix of "Oh that again".
编辑:
然后我会查看你的 show
代码。你的调用代码不在这里,但是如果我假设你只是使用你的 show
代码并且你的视图只在屏幕上显示3秒钟,那就是你的问题。
Then I'd look at your show
code. Your calling code isn't here, but if I'm to assume your are just using your show
code and your view is only on screen for 3 seconds, then that's you problem.
正如Justin提到的(上面的评论)和Apple的文档
As Justin mentioned (comment above) and Apple's docs
从整个时间开始您的视图位于屏幕上,它是动画块的一部分,所有交互将在其可见的整个时间内禁用。我从来没有完全测试延迟
位以及动画是否在该片段中被禁用,但在延迟期间禁用动画并不会让我感到惊讶。第二个动画仍在主动画块中,因此我假设动画将被阻止,直到两个动画完成。
Since the entire time your view is on the screen it's part of an animation block, all interaction will be disable for the entire time it's visible. I've never quite tested the delay
bit and whether animation was disabled during that piece, but it would not surprise me animation is disabled during the delay. The second animation is still inside the primary animation block, so I'd assume animations will be blocked until both are complete.
这篇关于UIView添加为UIWindow子视图不响应点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!