gpt4 book ai didi

swift - CollectionView 单元格继续

转载 作者:行者123 更新时间:2023-11-30 11:54:35 26 4
gpt4 key购买 nike

我在 CollectionView 单元格中的缩略图图像上有一个点击手势,当将其推送/呈现/等到另一个没有 Storyboard的 View Controller 时,它可以工作,因为我实现了 Storyboard,它不再工作了,我尝试了许多不同的解决方案,并且有是没有过渡。我将类(class)设置在 Storyboard上,但一切都是以编程方式完成的。我想知道是否有任何方法可以从当前 ViewController 切换到 CollectionView 单元格中的另一个。

Storyboard图片:

enter image description here

这就是模拟器中的样子:

enter image description here

import UIKit

class Cell: UICollectionViewCell{

override init(frame: CGRect) {
super.init(frame: frame)
setupViews()

let tapRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(Tap(recognizer:)))
thumbnailImage.isUserInteractionEnabled = true
thumbnailImage.addGestureRecognizer(tapRecognizer)

}

func setupViews(){
addSubview(thumbnailImage)
addSubview(separatorView)
addSubview(brandName)
addSubview(Priceing)
addSubview(model)


addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: thumbnailImage)
addConstraintsWithFormat(format: "H:|[v0]|", views: separatorView)

//Vertical Contsraint
addConstraintsWithFormat(format: "V:[v0(200)]-87-[v1(1)]|", views: thumbnailImage, separatorView)
addConstraintsWithFormat(format: "V:[v0(20)]", views: brandName)
addConstraintsWithFormat(format: "V:[v0(20)]", views: Priceing)
addConstraintsWithFormat(format: "V:[v0(20)]", views: model)

addConstraint(NSLayoutConstraint(item: brandName, attribute: .top, relatedBy: .equal, toItem: thumbnailImage, attribute: .bottom, multiplier: 1, constant: 8))
addConstraint(NSLayoutConstraint(item: brandName, attribute: .right, relatedBy: .equal, toItem: thumbnailImage, attribute: .right, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: brandName, attribute: .left, relatedBy: .equal, toItem: thumbnailImage, attribute: .left, multiplier: 1, constant: 0))

addConstraint(NSLayoutConstraint(item: Priceing, attribute: .top, relatedBy: .equal, toItem: brandName, attribute: .bottom, multiplier: 1, constant: 4))
addConstraint(NSLayoutConstraint(item: Priceing, attribute: .right, relatedBy: .equal, toItem: brandName, attribute: .right, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: Priceing, attribute: .left, relatedBy: .equal, toItem: brandName, attribute: .left, multiplier: 1, constant: 0))

addConstraint(NSLayoutConstraint(item: model, attribute: .top, relatedBy: .equal, toItem: Priceing, attribute: .bottom, multiplier: 1, constant: 4))
addConstraint(NSLayoutConstraint(item: model, attribute: .right, relatedBy: .equal, toItem: Priceing, attribute: .right, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: model, attribute: .left, relatedBy: .equal, toItem: Priceing, attribute: .left, multiplier: 1, constant: 0))
}

let separatorView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(displayP3Red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

let brandName: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211)
label.numberOfLines = 2
return label
}()

let model: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211)
label.numberOfLines = 1
return label
}()

let Priceing: UILabel = {
let textView = UILabel()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.numberOfLines = 1
textView.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211)
return textView
}()

let thumbnailImage: UIImageView = {
let image = UIImageView()
image.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211)
image.contentMode = .scaleAspectFill
image.clipsToBounds = true
return image
}()

@objc func Tap(recognizer: UITapGestureRecognizer){
let vc = DescriptionViewController()
// let nc = self.window?.rootViewController?.navigationController
// let transition = CATransition()
// transition.duration = 0.3
// transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
// transition.type = kCATransitionMoveIn
// transition.subtype = kCATransitionFromTop
// nc?.navigationController?.view.layer.add(transition, forKey: nil)
self.window?.rootViewController?.navigationController?.pushViewController(vc, animated: false)
}



required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

最佳答案

使用此扩展查找您的 UIViewUIViewController

extension UIView {

var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}

在您的项目中添加此扩展并像这样调用

    if let vc = self.parentViewController as? YourPresentedViewController{
let pushVc = DescriptionViewController()
vc.navigationController?.pushViewController(pushVc, animated: true)
}

注意:- UITableView 添加到 YourPresentedViewController 中,因此我的 tableview 父 View Controller 是 YourPresentedViewController。

关于swift - CollectionView 单元格继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47966041/

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