- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以将一个 UIImage 的掩码属性设置为另一个 UIView,但是如果我将 UIImage 的掩码属性设置为一个 UISwitch,则 UIImage 和 UISwitch 不会显示。
recordMicSwitch = UISwitch()
guard let sc = recordMicSwitch else { return }
sc.frame = CGRect(x: deviceWidth - sc.frame.size.width - rightMargin, y: yPos, width: 0, height: 0)
sc.onTintColor = UIColor(red: 0, green: 0.717, blue: 1.0, alpha: 1.0)
view.addSubview(sc)
let image: UIImage = UIImage(named: "testBGGradient.png")!
let bgImage = UIImageView(image: image)
bgImage.frame = CGRect(x:200, y:120, width:bgImage.frame.width/2, height:bgImage.frame.height/2)
bgImage.mask = recordMicSwitch!
self.view.addSubview(bgImage)
最佳答案
所以这实际上并不像将我们的图像屏蔽到 UISwitch
那样简单。
这种简单方法行不通的原因在于 mask 的实际工作方式。当我们像您建议的那样遮盖图像时,我们会采用另一个 View 的形状并将其应用于我们的图像。然后,我们的图像实际上被添加到父级。我们最终得到的是一个被切割成我们开关形状的图像(这个图像没有接收到任何开关事件)。
我们实际上要做的是更多地参与。我们需要将我们的图像添加到开关的 subview
的不同部分,并将其遮盖起来。
为了方便起见,我制作了一个自定义开关类来完成幕后的繁重工作:
class ImageTintSwitch: UISwitch {
init(tintImage: UIImage) {
super.init(frame: .zero)
// Make sure we have subviews & grab the first one
guard let element = subviews.first else { return }
// Loop through only the subviews that clipToBounds inside the one we grabbed
for (index, view) in element.subviews.enumerated() where view.clipsToBounds {
// Add our image only where we need it
configure(with: tintImage, on: element, maskedTo: view, atIndex: index)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func configure(with image: UIImage, on parent: UIView, maskedTo view: UIView, atIndex index: Int) {
// Make an imageView with our image
let imageView: UIImageView = {
let view = UIImageView(image: image)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
// Insert our new imageView only where we need it
parent.insertSubview(imageView, at: index)
// Mask our imageView to the views that we found
imageView.mask = view
// Constrain our imageView to match the parent view
NSLayoutConstraint.activate([
imageView.centerXAnchor.constraint(equalTo: parent.centerXAnchor),
imageView.centerYAnchor.constraint(equalTo: parent.centerYAnchor),
imageView.widthAnchor.constraint(equalTo: parent.widthAnchor),
imageView.heightAnchor.constraint(equalTo: parent.heightAnchor)
])
}
}
要使用这个自定义开关,我们只需使用以下代码:
let customSwitch = ImageTintSwitch(tintImage: UIImage(named: "gradient.jpg") ?? UIImage())
结果如下:
关于ios - 如何用 UIControl 屏蔽 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54770444/
我需要创建一个 View (作为其他 View 的 mask ) 。圆圈是透明的,外部区域是半透明的。我可以使用 canvas.clipRegion(..) 来实现它,但是 http://develo
踏入爬虫的迷宫 多年以来,我一直是一个对编程充满了好奇心的人。探索着代码的世界,便如同探险家踏入密林深处,寻找未知的宝藏。最近,我将目光聚焦在了爬虫技术上,特别是百度搜索屏蔽问题上。 百度搜索的诱惑
我想将“www.adomain.com”重定向到“www.adomain.com/cms”。 cms 部分应该被屏蔽。 我无法让它安静地工作。因此“cms”始终是网址的一部分。 我尝试了这个解决方案:
我一直在我的 View Controller 中使用以下代码: UIView *view = [[CustomView alloc] init]; UIView *mask = [[CustomMas
如果是网页内容里面的alert,我们可以等网页加载完毕,也就是在webViewDidFinishLoad中执行下面的js代码,就可以屏蔽alert了
我正在使用 PyQt4 QLineEdit小部件接受密码。有一个setMasking属性,但不遵循如何设置屏蔽字符。 最佳答案 editor = QLineEdit() editor.setEchoM
我有一个包含员工信息的 Pandas 数据框,如下所示: df=pd.DataFrame({ 'Id':[1,2,3,4], 'Name':['Joe','Henry','Sam','
我正在为 iPhone 创建一个自定义开/关切换开关(类似于标准开关),并且我正在设置 slider 的蒙版,但调用 [[myView [layer] setMask:maskLayer] 设置
我如何能够在 Objective C 中屏蔽 nsimageview?例如,有一个带圆角的 nsimageview。 最佳答案 你不知道。如果你想以 NSImageView 不支持的方式绘制图像,则需
我正在开发一个可以更改边框或矩形 UIImage 的应用程序。边框会有所不同,但看起来 UIImage 是用剪刀剪掉的,或者有什么影响。 做到这一点的最佳方法是什么? 我的第一个想法是准备一堆具有我正
我需要屏蔽数据,就像在 Azure Pipelines 中一样,但位于 Azure 存储库文件内。有没有一种方法可以设置与脚本分开存储的变量,例如在 Azure 管道中: variable = $(S
我没有找到解决问题的简单方法。我想使用 TextInputDialog,您必须在其中键入用户密码,以重置数据库中的所有数据。 TextInputDialog 的问题是它没有屏蔽文本,我不知道有什么选择
我正在寻找一种自行开发的方法来扰乱生产数据以用于开发和测试。我已经构建了几个脚本来生成随机社会安全号码、轮类出生日期、打乱电子邮件等。但我在尝试打乱客户姓名时遇到了困难。我想保留真实姓名,这样我们仍然
我正在尝试使用过滤器来屏蔽 SSN 的前 5 位数字,它应该看起来像这样 XXX-XX-1234 到目前为止我想出了什么: // {{SocialSecurityNumber | ssn}} angu
我没有找到解决问题的简单方法。我想使用 TextInputDialog,您必须在其中键入用户密码,以重置数据库中的所有数据。 TextInputDialog 的问题是它没有屏蔽文本,我不知道有什么选择
我想屏蔽一个 Fortran 数组。这是我目前正在做的方式...... where (my_array <=15.0) mask_array = 1 elsewhere mask_ar
当用户在用户界面上输入时,我需要屏蔽数字,用户应该看到一个屏蔽的数字,但在 Java 代码上,我应该得到整个数字,包括屏蔽的字符用户应该看到什么 4545********9632但在 Java 代码(
我正在使用 javafx 文本字段。我需要输入数字以及应符合格式 ###.###.###.### 的小数其中小数点根本不可编辑,数字可以是任何数字;不允许其他输入。并非所有数字占位符都需要填写,例如它
我必须交付一个小型 C++ 软件。我想用一些实现(我使用了表达式模板)来掩盖头文件,以使最终用户难以辨认,因此他们无法修改它们。我无法将代码包含在静态或动态库中。有什么方法可以屏蔽头文件的内容吗? 谢
我正在开发一个与 gestureRecognizer 配合使用的应用程序。 使用手势可以选择 UIImage(例如 rectangle.png),并且可以使用 UIPopoverView 通过为所选图
我是一名优秀的程序员,十分优秀!