我添加了一个UIView
含有一个UITapGestureRecognizer
作为我的关键窗口的子视图。它显示正确,但是当我点击视图时,目标方法不会被触发。我什至尝试用手势识别器替换UIButton
,仍然无济于事。
这是我的代码。
通知视图.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
通知视图.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
没有剪切到边界,但我无法与任何东西交互,因为我的点击超出了边界UIView
.
我的简单测试是改变背景颜色UIView
看看它是否覆盖了我期望的区域,或者我是否以某种方式搞砸了尺寸/位置。
我曾经因为这个问题而把头撞到墙上,挣扎了几个小时,但我已经这样做了很多次了,现在只需 5 分钟就能解决“哦,又是这样”。
Edit:
然后我会看看你的show
代码。您的调用代码不在这里,但如果我假设您只是使用您的show
如果你的代码只在屏幕上显示了 3 秒,那么这就是你的问题了。
正如贾斯汀提到的(上面的评论)和苹果的文档
在动画播放期间,用户交互会暂时禁用
动画中涉及的所有视图,无论其值如何
财产。您可以通过指定来禁用此行为
配置时的 UIViewAnimationOptionAllowUserInteraction 选项
动画片。
由于视图在屏幕上的整个时间都是动画块的一部分,因此在其可见的整个时间内所有交互都将被禁用。我从来没有完全测试过delay
位以及动画是否在那段期间被禁用,但我不会感到惊讶动画在延迟期间被禁用。第二个动画仍在主动画块内,因此我假设动画将被阻止,直到两个动画都完成为止。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)