[英]UIView doesn't respond to touches
I have simple view controller and I add this controller like subview for window using this code:我有简单的视图控制器,我使用以下代码添加了这个控制器,如窗口的子视图:
UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (!window)
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
self.view.alpha = 0.0;
self.view.userInteractionEnabled = YES;
[window addSubview:self.view];
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeTriggered:)]];
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.view.alpha = 1.0;
}completion:nil];
But when I click at this view - nothing happens.但是当我点击这个视图时 - 没有任何反应。 Even event touchesBegan: are not called.甚至事件 touchesBegan: 都不会被调用。
UPD: Code above is in -(void)show method. UPD:上面的代码在 -(void)show 方法中。 I want to show one controller above all controllers.我想在所有控制器上方显示一个控制器。 In FirstViewController I create instance of CustonAlertViewController like that:在 FirstViewController 中,我创建了 CustonAlertViewController 的实例,如下所示:
CustomAlertViewController *alertVC = [[CustomAlertViewController alloc]init];
[alertVC show];
In CustomAlertViewController I have show method presented at the top and viewDidLoad method with:在 CustomAlertViewController 中,我在顶部和 viewDidLoad 方法中显示了显示方法:
self.view.backgrondColor = [UIColor greenColor];
Hidden views (and equivalently, those with alpha == 0.0
) do not respond to touches.隐藏视图(等效地,那些alpha == 0.0
)不响应触摸。 If you require a fully transparent view, leave alpha as > 0.0, and say...如果您需要完全透明的视图,请将 alpha 保留为 > 0.0,然后说...
self.view.backgroundColor = [UIColor clearColor];
Alternatively, assign a nonzero alpha.或者,分配一个非零 alpha。
EDIT yes, the CustomAlertViewController
instance is being deallocated immediately after show
is invoked.编辑是的,在调用show
后立即释放CustomAlertViewController
实例。 The view controller that does the allocating needs to have a strong property to keep the alert around,执行分配的视图控制器需要有一个强大的属性来保持警报,
@property(nonatomic,strong) CustomAlertViewController *alertVC;
and adding...并添加...
CustomAlertViewController *alertVC = [[CustomAlertViewController alloc]init];
self.alertVC = alertVC;
[alertVC show];
This doesn't try to address some potential problems beyond the scope of this question (like rotation, or cleanly restoring when the alert is done).这并不试图解决超出此问题范围的一些潜在问题(例如轮换,或在警报完成后干净利落地恢复)。
// try like this , u need to give number of touches // 像这样尝试,你需要给出触摸次数
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeTriggered:)];
gesture.numberOfTapsRequired = 1;
gesture.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:gesture];
You have been setting your UITapGestureRecognizer
after you addSubview
to your window.您已设置您UITapGestureRecognizer
你以后addSubview
到你的窗口。 like:喜欢:
You have been doing this你一直在这样做
//...
[window addSubview:self.view];
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeTriggered:)]];
//...
Try this尝试这个
Set UITapGestureRecognizer
before you addSubview
.在addSubview
之前设置UITapGestureRecognizer
。 And remember to add the UIGestureRecognizerDelegate
to your ViewController
并记住将UIGestureRecognizerDelegate
添加到您的ViewController
self.view.alpha = 0.2;
self.view.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeTriggered:)];
tapGesture.numberOfTapsRequired = 1;
[tapGesture setDelegate:self];
[self.view addGestureRecognizer:tapGesture];
// After you add your self.view to window
[window addSubview:self.view];
I hope this can help you.我希望这可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.