gpt4 book ai didi

ios - 如何聚焦 UIView?

转载 作者:行者123 更新时间:2023-11-29 06:02:27 25 4
gpt4 key购买 nike

我想实现单选按钮等功能。更具体地说,我想实现从多个 UIView 中仅选择一个 UIView 的功能。这类似于 Focus EnginetvOS上。

enter image description here

在搜索相关内容时,我注意到 UIKit 支持 Focus-based Navigation 。但我不确定这是否完全支持我想要的。还缺乏其他相关示例。

我想听到一些有关相关功能的帮助和建议。 基于焦点的导航是否适合我所追求的目的?还有其他好的方法来实现我想要实现的功能吗?

最佳答案

@Paulw 感谢您的帮助。

以下步骤解决了问题!

我使用了一种简单的方法来影响多个UIViews中指定的UIView

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
}
}
}
}

关于ios - 如何聚焦 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54490100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com