快速选择的标签
我正在开发带有地图的应用程序。现在,我从底部构建了一个模态表,我想实现地图类型选择(例如Google Maps)。
问题是我不确定如何构建这样的按钮的可选列表:
我不了解的是我需要使用哪种类型的对象?因为我试图用按钮构建它,但我不确定这是一个很好的解决方案。
import UIKit
@objc(LayerViewController)
public class LayerViewController: UIViewController {
var selectedIndex: Int = 0
var normal: UIButton = UIButton()
var satellite: UIButton = UIButton()
var style: [UIButton] = []
override public func viewDidLoad() {
super.viewDidLoad()
style.append(normal)
style.append(satellite)
setStyle()
}
func setStyle() {
normal.frame = CGRect(
x: 100,
y: self.view.frame.height * 0.2,
width: 100,
height: 100
)
normal.setBackgroundImage(UIImage(named: "background"), for: .normal)
normal.layer.borderColor = .init(red: 0, green: 0, blue: 255, alpha: 1)
normal.layer.borderWidth = 5
normal.isSelected = true
normal.addTarget(self, action: #selector(selectNormal), for: .touchUpInside)
satellite.frame = CGRect(
x: 300,
y: self.view.frame.height * 0.2,
width: 100,
height: 100
)
satellite.setBackgroundImage(UIImage(named: "background"), for: .normal)
satellite.isSelected = false
satellite.addTarget(self, action: #selector(selectedSatellite), for: .touchUpInside)
self.view.addSubview(normal)
self.view.addSubview(satellite)
}
@objc func selectNormal() {
if (selectedIndex != 0) {
normal.layer.borderColor = .init(red: 0, green: 0, blue: 255, alpha: 1)
normal.layer.borderWidth = 5
normal.isSelected = true
style[selectedIndex].layer.borderColor = .init(red: 0, green: 0, blue: 0, alpha: 0)
style[selectedIndex].layer.borderWidth = 0
style[selectedIndex].isSelected = false
selectedIndex = 0
}
}
@objc func selectedSatellite() {
if (selectedIndex != 1) {
satellite.layer.borderColor = .init(red: 0, green: 0, blue: 255, alpha: 1)
satellite.layer.borderWidth = 5
satellite.isSelected = true
style[selectedIndex].layer.borderColor = .init(red: 0, green: 0, blue: 0, alpha: 0)
style[selectedIndex].layer.borderWidth = 0
style[selectedIndex].isSelected = false
selectedIndex = 1
}
}
}
编辑: 该解决方案似乎有效,但我不确定这是一个真正的解决方案。因为如果我需要添加更多样式,我应该写很多代码。
I'm developing an application with a map. Now I build a modal sheet from bottom and I want to implement the map type selection (like google maps).
The problem is that I'm not sure how to build the selectable list of button like this one:
What I'm not understanding is which type of object I need to use? Because I'm trying to build it with buttons but I'm not sure that this is a great solution.
import UIKit
@objc(LayerViewController)
public class LayerViewController: UIViewController {
var selectedIndex: Int = 0
var normal: UIButton = UIButton()
var satellite: UIButton = UIButton()
var style: [UIButton] = []
override public func viewDidLoad() {
super.viewDidLoad()
style.append(normal)
style.append(satellite)
setStyle()
}
func setStyle() {
normal.frame = CGRect(
x: 100,
y: self.view.frame.height * 0.2,
width: 100,
height: 100
)
normal.setBackgroundImage(UIImage(named: "background"), for: .normal)
normal.layer.borderColor = .init(red: 0, green: 0, blue: 255, alpha: 1)
normal.layer.borderWidth = 5
normal.isSelected = true
normal.addTarget(self, action: #selector(selectNormal), for: .touchUpInside)
satellite.frame = CGRect(
x: 300,
y: self.view.frame.height * 0.2,
width: 100,
height: 100
)
satellite.setBackgroundImage(UIImage(named: "background"), for: .normal)
satellite.isSelected = false
satellite.addTarget(self, action: #selector(selectedSatellite), for: .touchUpInside)
self.view.addSubview(normal)
self.view.addSubview(satellite)
}
@objc func selectNormal() {
if (selectedIndex != 0) {
normal.layer.borderColor = .init(red: 0, green: 0, blue: 255, alpha: 1)
normal.layer.borderWidth = 5
normal.isSelected = true
style[selectedIndex].layer.borderColor = .init(red: 0, green: 0, blue: 0, alpha: 0)
style[selectedIndex].layer.borderWidth = 0
style[selectedIndex].isSelected = false
selectedIndex = 0
}
}
@objc func selectedSatellite() {
if (selectedIndex != 1) {
satellite.layer.borderColor = .init(red: 0, green: 0, blue: 255, alpha: 1)
satellite.layer.borderWidth = 5
satellite.isSelected = true
style[selectedIndex].layer.borderColor = .init(red: 0, green: 0, blue: 0, alpha: 0)
style[selectedIndex].layer.borderWidth = 0
style[selectedIndex].isSelected = false
selectedIndex = 1
}
}
}
EDIT:
This solution seems working but I'm not sure that this is a real solution. Because if I need to add more style I should write a lot of code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在想类似的事情:
I was thinking to something like :