简体   繁体   中英

How do I focus UIView?

I want to implement functions like radio buttons . More specifically, I want to implement the ability to select only one UIView from several UIView s. This is similar to the Focus Engine on tvOS .

在此处输入图片说明

While searching of relevant this, I noticed that UIKit supports Focus-based Navigation . But I am not sure if this supports exactly what I want. There is also a lack of additional relevant examples.

I would like to hear some help and advice on related features. Is the Focus-based Navigation suitable for the purpose I was pursuing? And are there any other good ways to implement the functionality I want to implement?

@Paulw Thank you for your kind help.

The following steps solved the problem!

I have used a simple way that effects a specified UIView among multiple 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
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM