【问题标题】:IOS:Remove view when clicked in backgroundIOS:在后台单击时删除视图
【发布时间】:2014-12-31 07:54:47
【问题描述】:

我正在做与自定义警报相关的应用程序。当我单击警报视图上的确定按钮时,用于显示具有透明背景的警报视图的书面代码 - 警报消失。

当我触摸透明视图时,同样的事情发生时我需要帮助,我的代码如下 :

- (void)didCustomPopUpAlertLoad:(UIView *)parentView andtitle:(NSString *)strTitle {

[self setRootView:parentView];
self.lblAlertMessage.text = strTitle;

//Add alertview into transparent view to hide parent view interaction
UIView *transparentView = [[UIView alloc] initWithFrame:parentView.bounds];
[transparentView setBackgroundColor:[UIColor clearColor]];
[transparentView addSubview:self];

float x = (int)(transparentView.bounds.size.width - self.bounds.size.width)>>1;
float y = (int)(transparentView.bounds.size.height - self.bounds.size.height)>>2;
[self setFrame:CGRectMake(x, y+62, self.bounds.size.width, self.bounds.size.height)];

//    [self setFrame:CGRectMake(x+10, y+62, self.bounds.size.width, self.bounds.size.height)];
[self.window addSubview:transparentView];
[self.window makeKeyAndVisible];
[[transparentView subviews]
 makeObjectsPerformSelector:@selector(setUserInteractionEnabled:)
 withObject:[NSNumber numberWithBool:FALSE]];


}
 -(void)didCustomPopUpUnload{

[self.superview removeFromSuperview];
// Set up the fade-in animation
self.window = nil;

 }

 -(IBAction)didActionOkAlertPopUp:(id)sender{

[self didCustomPopUpUnload];
 }

【问题讨论】:

  • 在透明视图上添加点击手势。
  • 如果您的透明视图的大小不等于窗口的大小,当您触摸透明视图之外的区域时,您将永远无法检测到触摸。如果无法全屏透明视图,则应学习 hitTest,否则只需添加手势或覆盖 touches 事件。
  • 如果你不能全屏,你应该在当前窗口添加另一个透明视图来测试触摸,并覆盖方法'hitTest:withEvent:'
  • 我有透明视图,即当我单击透明视图时它应该消失的父视图的大小。请提供代码的 sn-p

标签: ios objective-c touch-event


【解决方案1】:

创建一个自定义的透明视图然后覆盖这个方法

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

将您的警报视图添加到此视图中,并将此视图用作全屏视图。 您可以使用event 参数进行计算以了解用户在alertview 内部或外部的触摸。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    相关资源
    最近更新 更多