我想实现单选按钮之类的功能。更具体地说,我想实现从几个UIView
中仅选择一个UIView
的功能。这类似于 tvOS 上的Focus Engine。
在搜索相关内容时,我注意到UIKit
支持Focus-based Navigation。但是我不确定这是否完全支持我想要的。也缺少其他相关示例。
我想听听有关相关功能的帮助和建议。 基于焦点的导航是否适合我追求的目标?还有其他好的方法可以实现我要实现的功能吗?
答案 0 :(得分:0)
@Paulw谢谢你的热心帮助。
以下步骤解决了该问题!
我使用了一个简单的方式影响特定UIView
多个间UIViews
。
import UIKit
class ViewController: UIViewController {
var selectView: UIView?
override func viewDidLoad() {
super.viewDidLoad()
selectView = self.view
let viw = UIView(frame: CGRect(x: 100, y: 100, width: 150, height: 150))
viw.backgroundColor = UIColor.white
viw.layer.cornerRadius = 10
self.view.addSubview(viw)
let objectView = ObjectView()
objectView.frame.size = CGSize(width: 150, height: 150)
objectView.backgroundColor = UIColor.clear
self.view.addSubview(objectView)
let tapObject = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
objectView.addGestureRecognizer(tapObject)
let tapObjects = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
viw.addGestureRecognizer(tapObjects)
let tapRootView = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
self.view.addGestureRecognizer(tapRootView)
}
@objc func handleTap(sender: UITapGestureRecognizer) {
if sender.state == .ended {
if selectView != self.view {
selectView?.layer.shadowColor = UIColor.clear.cgColor
}
selectView = sender.view
if selectView != self.view {
sender.view?.layer.shadowOffset = .zero
sender.view?.layer.shadowOpacity = 0.5
sender.view?.layer.shadowColor = UIColor.black.cgColor
}
}
}
}