spe*_*f10 6 objective-c uiview ios
我有一个UIView,当我点击按钮时我出现了,我基本上将它用作自定义警报视图.现在,当用户点击我添加到主视图的自定义UIView之外时,我想要隐藏cusomt视图,我可以轻松地执行此操作customView.hidden = YES;但是如何检查视图外的水龙头?
谢谢您的帮助
有两种方法:
您可以为自定义视图设置标记:
customview.tag=99;
Run Code Online (Sandbox Code Playgroud)
然后在viewcontroller中使用touchesBegan:withEvent:委托
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if(touch.view.tag!=99){
customview.hidden=YES;
}
}
Run Code Online (Sandbox Code Playgroud)
更有可能的是,每次你想要弹出一个自定义视图时,它背后都有一个叠加层,它将填满你的屏幕(例如一个alpha~0.4的黑色视图).在这些情况下,您可以添加一个UITapGestureRecognizer,并在每次希望显示自定义视图时将其添加到视图中.这是一个例子:
UIView *overlay;
-(void)addOverlay{
overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 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];
}];
}
Run Code Online (Sandbox Code Playgroud)
当您呈现自定义警报视图时,将该自定义警报视图添加到另一个全屏视图中,通过设置其backgroundColor清晰来使该视图清晰。在主视图中添加全屏视图,并tapGesture在全屏不可见视图中添加,当它被点击时删除此视图。
但是,如果您这样做,即使您触摸自定义警报视图,它也会关闭视图,因为您需要设置委托tapGesture并实现此方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isDescendantOfView:self.customAlertView])
{
return NO;
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5961 次 |
| 最近记录: |