创建可在 swift 中交互的不可见视图

Create an invisible view that is interactable in swift

对于原型设计,我希望创建一个不可见的交互区域。

我找到的最佳解决方案是将背景色设置为不可见的颜色。您看不到按钮,但可以与之交互。

在初始化中我输入:

self.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0)

有更好的方法吗?

创建一个 UIView 启用 userInteraction 并清除背景颜色

UIView *viewsample=[[UIView alloc]init];
viewsample.frame= CGRectMake(0, 0, 200, 200);
viewsample.backgroundColor= [UIColor clearColor];
viewsample.userInteractionEnabled=YES;
[self.view addSubview:viewsample];

UITapGestureRecognizer *tapSkip = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iviewsampleClicked:)];
[viewsample addGestureRecognizer:tapSkip];

触摸时调用的方法

-(void)iviewsampleClicked:(UIGestureRecognizer*)gesture{

}

为什么要为此使用视图,相反,您可以只使用 UIButton 并将框架设置为当前视图的框架并将其背景颜色设置为 clearColor,如下所示,

self.invisibleButton.backgroundColor = [UIColor clearColor];

这将执行相同的操作并减少一些小工作,例如添加点击手势和设置视图的一些属性(如果您使用视图)。