swift - 在点击时切换 UIViews 类
问题描述
UIViews
我在 a 中有 5 个不同View Controller
,我想通过更改其边框颜色仅突出显示最后一个被点击的。
我做了一个Custom Highlighting Class
在设计方面封装所需效果的,并给每个 UIView aUITapGestureRecognizer
及其#selector(toggleFunction)
. 这 5 个UIViews
已经分配了一个类,所以基本上我需要将 更改Original Class
为Custom Class
,或者只是根据需要打开/关闭它.
我的问题是如何在 5 之间点击时打开和关闭此自定义类UIViews
?
解决方案
你可以试试
extension UIView {
func addBorder(_ add:Bool) {
self.layer.borderColor = add ? UIColor.red.cgColor : UIColor.green.cgColor
self.layer.borderWidth = add ? 5 : 0
}
}
@objc func tapped(_ v:UITapGestureRecognizer) {
let currentView = v.view!
allViews.forEach {
$0.addBorder($0 == currentView)
}
}
假设你有
var allViews = [UIView]()
推荐阅读
- laravel - 完成运行后停止 Laravel 队列
- amazon-web-services - AWS:无法连接到端口 XXXX:连接被拒绝
- vba - 如何刷新访问前端的数据?
- php - 使用 htaccess 自定义 URL 重写
- angular - Angular loadChildren 使用数据创建选项卡/导航
- javascript - 反应js如何在写入csv并给出粗体标题时将文本左对齐
- wordpress - 如何以编程方式将用户登录到 Wordpress?
- c++ - C++ 正则表达式捕获正在删除电子邮件验证器中的最后一个字符
- javascript - JavaScript 相当于 JQuery AJAX 请愿书?
- vue.js - 如何在 VueJS 中使用 Vuetify 制作对话框形式?