我有这个 TatodexController 来处理我的 Collection View 代码。它从 API 调用中获取数据并将其保存在 pokemon var 中。这个 var 使用 Pokemon 结构体进行解码,该结构体包含每个 pokemon 的所有属性(名称、身高、能力等)。
在我的 CollectionView 上,我可以显示与戳相关的图像、名称和更多信息,但我添加了一个以这种方式工作的功能:
- 当我长按任何口袋妖怪单元格时,它会显示一个信息视图,它应该显示一大块口袋妖怪信息,但它没有从我的 TatodexController 获取更新的数据。
我的 TatodexController 的相关代码如下所示:
class TatodexController: UICollectionViewController, InfoViewDelegate {
var pokemon: Pokemon?
var pokemons = [Pokemon]()
let service = Service()
. . .
let infoView: InfoView = {
let view = InfoView()
view.layer.cornerRadius = 5
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
fetchPokemons()
}
. . .
@objc func handleDismissal() {
dismissInfoView(pokemon: nil)
}
}
我处理解析和一些 InfoView 配置的扩展是这个:
extension TatodexController {
. . .
func dismissInfoView(pokemon: Pokemon?) {
UIView.animate(withDuration: 0.5, animations: {
self.visualEffectView.alpha = 0
self.infoView.alpha = 0
self.infoView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
}) { (_) in
self.infoView.removeFromSuperview()
self.navigationItem.rightBarButtonItem?.isEnabled = true
guard let pokemon = pokemon else { return }
self.showInfoController(withPoke: pokemon)
}
}
func fetchPokemons() {
service.fetchPokes { (result) in
DispatchQueue.main.async {
switch result {
case .success(let poke):
self.pokemon = poke
self.pokemons.append(poke)
self.collectionView.reloadData()
self.pokemons.sort { (poke1, poke2) -> Bool in
return poke1.name! < poke2.name!
}
case .failure(let error):
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
self.show(alert, sender: nil)
}
}
}
}
}
配置我的 InfoViewDelegate 的扩展是:
extension TatodexController: TatodexCellDelegate {
func presentInfoView(withPokemon pokemon: Pokemon) {
configureSearchBar(showSearch: false)
navigationItem.rightBarButtonItem?.isEnabled = false
view.addSubview(infoView)
infoView.configureViewComponents()
infoView.delegate = self
infoView.pokemon = self.pokemon
configureInfoView()
}
func configureInfoView() {
infoView.anchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: view.frame.width - 64, height: 480)
infoView.layer.cornerRadius = view.frame.width / 6
infoView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
infoView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -40).isActive = true
infoView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
infoView.alpha = 0
UIView.animate(withDuration: 0.3) {
self.visualEffectView.alpha = 1
self.infoView.alpha = 1
self.infoView.transform = .identity
}
}
}
我的 InfoView 类的相关代码是这样的:
protocol InfoViewDelegate {
func dismissInfoView(pokemon: Pokemon?)
}
class InfoView: UIView {
var delegate: InfoViewDelegate?
var pokemon: Pokemon? {
didSet {
guard let pokemon = self.pokemon,
let type = pokemon.types,
let typeName = type[0].type?.name,
let defense = pokemon.defense,
let attack = pokemon.attack,
let id = pokemon.id,
let height = pokemon.height,
let weight = pokemon.weight,
let imageUrl = pokemon.sprites?.front else { return }
if id == pokemon.id {
imageView.kf.setImage(with: URL(string: imageUrl))
}
nameLabel.text = pokemon.name?.capitalized
configureLabel(label: typeLabel, title: "Type", details: "\(typeName)")
configureLabel(label: defenseLabel, title: "Defense", details: "\(defense)")
configureLabel(label: attackLabel, title: "Base Attack", details: "\(attack)")
configureLabel(label: heightLabel, title: "Height", details: "\(height)")
configureLabel(label: weightLabel, title: "Weight", details: "\(weight)")
configureLabel(label: pokedexIdLabel, title: "Pokedex Id", details: "\(id)")
}
}
let skillLabel: UILabel = {
let label = UILabel()
return label
}()
let imageView: UIImageView = {
let iv = UIImageView()
iv.contentMode = .scaleAspectFill
return iv
}()
lazy var nameContainerView: UIView = {
let view = UIView()
view.backgroundColor = Colors.softRed
view.addSubview(nameLabel)
view.layer.cornerRadius = 5
nameLabel.center(inView: view)
return view
}()
let nameLabel: UILabel = {
let label = UILabel()
label.textColor = .white
label.font = UIFont.systemFont(ofSize: 24, weight: .thin)
label.text = "Lucario"
return label
}()
. . .
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func handleViewMoreInfo() {
guard let pokemon = self.pokemon else { return }
delegate?.dismissInfoView(pokemon: pokemon)
}
. . .
}
当然,在最后一段代码中,我没有获取数据。我认为这是因为我没有将我的 TatodexController 的口袋妖怪信息与我的 InfoView 连接起来,但我不知道该怎么做。例如,当我长按任何 pokemon 单元格并显示 InfoView 时,它会显示“Lucario”(这是我的默认值),而不是实际的 pokemon 名称。
我将非常感谢任何帮助或建议。如果有人需要查看另一块代码,我很乐意更新这篇文章。
编辑 1:TatodexCell 的 handleLongPress 函数
@objc func handleLongPress(sender: UILongPressGestureRecognizer) {
if sender.state == .began {
guard let poke = self.pokemon else { return }
delegate?.presentInfoView(withPokemon: poke)
}
}