我有一个UIView,当我点击按钮时我出现了,我基本上将它用作自定义警报视图.现在,当用户点击我添加到主视图的自定义UIView之外时,我想隐藏cusomt视图,我可以使用customView.hidden = YES轻松完成此操作;但是如何检查视图外的水龙头?
谢谢您的帮助
解决方法
有两种方法:
第一种方法:
customview.tag=99;
然后在viewcontroller中,使用touchesBegan:withEvent:delegate
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; if(touch.view.tag!=99){ customview.hidden=YES; } }
第二种方法:
每次你想要弹出一个自定义视图时,它更有可能在它背后有一个叠加层,它将填满你的屏幕(例如一个alpha~0.4的黑色视图).在这些情况下,您可以向其添加UITapGestureRecognizer,并在每次希望显示自定义视图时将其添加到视图中.这是一个例子:
UIView *overlay; -(void)addOverlay{ overlay = [[UIView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.width,self.view.frame.size.height)]; [overlay setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]]; UITapGestureRecognizer *overlayTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onOverlayTapped)]; [overlay addGestureRecognizer:overlayTap]; [self.view addSubview:overlay]; } - (void)onOverlayTapped { NSLog(@"Overlay tapped"); //Animate the hide effect,you can also simply use customview.hidden=YES; [UIView animateWithDuration:0.2f animations:^{ overlay.alpha=0; customview.alpha=0; }completion:^(BOOL finished) { [overlay removeFromSuperview]; }]; }