我有简单的视图控制器,我使用此代码添加此控制器,如窗口的子视图:
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];
但是当我点击这个视图时 - 没有任何反应。即使是事件触摸开始:也没有被调用。
UPD: 上面的代码是 - (void)show方法。我想在所有控制器上方显示一个控制器。 在FirstViewController中,我创建了CustonAlertViewController的实例:
CustomAlertViewController *alertVC = [[CustomAlertViewController alloc]init];
[alertVC show];
在CustomAlertViewController中,我在顶部显示show方法,在viewDidLoad方法中显示:
self.view.backgrondColor = [UIColor greenColor];
答案 0 :(得分:1)
隐藏的视图(以及等同于alpha == 0.0
的视图)不响应触摸。如果您需要完全透明的视图,请将alpha保留为> 0.0,并说...
self.view.backgroundColor = [UIColor clearColor];
或者,指定非零alpha。
编辑是的,CustomAlertViewController
实例在调用show
后立即被释放。执行分配的视图控制器需要具有强大的属性来保持警报,
@property(nonatomic,strong) CustomAlertViewController *alertVC;
并添加......
CustomAlertViewController *alertVC = [[CustomAlertViewController alloc]init];
self.alertVC = alertVC;
[alertVC show];
这不会试图解决超出此问题范围的一些潜在问题(例如轮换,或在完成警报时干净地恢复)。
答案 1 :(得分:0)
//尝试这样,你需要给出一些触摸
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeTriggered:)];
gesture.numberOfTapsRequired = 1;
gesture.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:gesture];
答案 2 :(得分:0)
您已将UITapGestureRecognizer
设置在addSubview
后窗口。像:
您一直在这样做
//...
[window addSubview:self.view];
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeTriggered:)]];
//...
试试这个
在UITapGestureRecognizer
之前设置addSubview
。请务必将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];
我希望这可以帮到你。