CollectionView 单元格转场
Posted
技术标签:
【中文标题】CollectionView 单元格转场【英文标题】:CollectionView Cell segue 【发布时间】:2017-12-25 05:49:42 【问题描述】:我在 CollectionView 单元格中的 thumbnailImage 上有一个点击手势,当我现在将它推送/呈现/等到另一个没有情节提要的视图控制器时,它可以工作,因为我实现了情节提要它不再起作用,我尝试了许多不同的解决方案是没有过渡。我在故事板上设置了课程,但一切都是以编程方式完成的。我想知道是否有任何方法可以从 CollectionView 单元格从当前 ViewController 切换到另一个。
故事板图片:
这是模拟器中的样子:
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")
【问题讨论】:
不要使用UICollectionViewCell
类,而是使用UIViewController
类进行导航。
@RichieRich 我在那个 thumbnailImage 上有一个特定的手势,我将如何在 UIViewController 上执行它。如果我确实在 UIViewController 上有一个点击手势,那将如何工作,因为我在 UIViewController 类上只有一个 CollectionView 和一个 UIButton。
如果您可以分享任何演示项目,我可以尝试提供帮助。
【参考方案1】:
使用此扩展程序查找您的 UIView
父母 UIViewController
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 parentViewController 是 YourPresentedViewController。
【讨论】:
if 语句被调用但它没有被推送它在 let pushVC 处停止 pushVc 表示你的目标视图控制器DescriptionViewController
这就是我所做的,我将其设置为 didSelectAt indexPath 函数,对于 ParentViewController 的扩展,我将其设置为 MainfeedViewController以上是关于CollectionView 单元格转场的主要内容,如果未能解决你的问题,请参考以下文章