编辑:我在正确的轨道上,但按钮只能指向1个ViewController
我用过
@IBAction func viewTouchedBttn(_ sender: Any) {
print("touched")
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
switch indexPath.row {
case 0:
print("1")
performSegue(withIdentifier: "1", sender: nil)
case 1:
performSegue(withIdentifier: "2", sender: nil)
case 2:
performSegue(withIdentifier: "3", sender: nil)
default:
break
}
}
我最近完成了关于使用本教程https://www.youtube.com/watch?v=vB-HKnhOgl8中教导的UICollectionView在swift中构建旋转木马的教程(您也可以下载它,因此我们将拥有相同的基础)
我已经不再使用编码了,所以我很抱歉,如果这对你来说显而易见,我就不会得到它^^。
每个类别应通过单击单元格指向不同的vc。
当我按下"观看世界" segue应该指向你可以谈论Travel的VC,如果我有兴趣构建应用程序并点击该视图,我希望被引导到这个特定的类别。
感谢您的帮助! :)
编辑:文件
InterestCollectionView
class InterestCollectionViewCell: UICollectionViewCell
{
@IBOutlet weak var featuredImageView: UIImageView!
@IBOutlet weak var interestTitleLabel: UILabel!
@IBOutlet weak var backgroundColorView: UIView!
@IBOutlet weak var kategorie: UILabel!
var interest: Interest? {
didSet {
self.updateUI()
}
}
private func updateUI()
{
if let interest = interest {
kategorie.text = interest.kategorie
featuredImageView.image = interest.featuredImage
interestTitleLabel.text = interest.title
backgroundColorView.backgroundColor = interest.color
} else {
featuredImageView.image = nil
interestTitleLabel.text = nil
backgroundColorView.backgroundColor = nil
}
}
override func layoutSubviews() {
super.layoutSubviews()
self.layer.cornerRadius = 3.0
layer.shadowRadius = 10
layer.shadowOpacity = 0.4
layer.shadowOffset = CGSize(width: 5, height: 10)
self.clipsToBounds = false
}
}
兴趣档案
class Interest
{
// MARK: - Public API
var kategorie = ""
var title = ""
var featuredImage: UIImage
var color: UIColor
init(title: String, featuredImage: UIImage, color: UIColor, kategorie: String)
{
self.kategorie = kategorie
self.title = title
self.featuredImage = featuredImage
self.color = color
}
// MARK: - Private
// dummy data
static func fetchInterests() -> [Interest]
{
return [
Interest(title: "Travelling Around the World", featuredImage: UIImage(named: "f1")!, color: UIColor(red: 63/255.0, green: 71/255.0, blue: 80/255.0, alpha: 0.8), kategorie: "Forum"),
Interest(title: "Cafe with Best Friends", featuredImage: UIImage(named: "f2")!, color: UIColor(red: 240/255.0, green: 133/255.0, blue: 91/255.0, alpha: 0.8), kategorie: "Forum"),
Interest(title: "Study Personal Development Books and Courses", featuredImage: UIImage(named: "f3")!, color: UIColor(red: 105/255.0, green: 80/255.0, blue: 227/255.0, alpha: 0.8), kategorie: "Post"),
Interest(title: "Build Amazing iOS Apps", featuredImage: UIImage(named: "f4")!, color: UIColor(red: 102/255.0, green: 102/255.0, blue: 102/255.0, alpha: 0.8), kategorie: "Forum"),
Interest(title: "Learn. Create. Contribute.", featuredImage: UIImage(named: "f5")!, color: UIColor(red: 245/255.0, green: 62/255.0, blue: 40/255.0, alpha: 0.8),kategorie: "Forum"),
Interest(title: "Inspire, Instruct, and Empower People", featuredImage: UIImage(named: "f6")!, color: UIColor(red: 103/255.0, green: 217/255.0, blue: 87/255.0, alpha: 0.8), kategorie: "Bilder"),
Interest(title: "Business and Marketing Geeks", featuredImage: UIImage(named: "f7")!, color: UIColor(red: 63/255.0, green: 71/255.0, blue: 80/255.0, alpha: 0.8), kategorie: "Forum"),
Interest(title: "3D Printing, Virtual Reality and AI", featuredImage: UIImage(named: "f8")!, color: UIColor(red: 240/255.0, green: 133/255.0, blue: 91/255.0, alpha: 0.8), kategorie: "Forum"),
Interest(title: "Einzeiler", featuredImage: UIImage(named: "f3")!, color: UIColor(red: 30/255.0, green: 20/255.0, blue: 130/255.0, alpha: 0.8), kategorie: "Forum")
]
}
}
兴趣ViewController
import UIKit
class InterestsViewController: UIViewController
{
@IBOutlet weak var collectionView: UICollectionView!
var interests = Interest.fetchInterests()
let cellScaling: CGFloat = 0.6
override func viewDidLoad() {
super.viewDidLoad()
let screenSize = UIScreen.main.bounds.size
let cellWidth = floor(screenSize.width * cellScaling)
let cellHeight = floor(screenSize.height * cellScaling)
let insetX = (view.bounds.width - cellWidth) / 2.0
let insetY = (view.bounds.height - cellHeight) / 2.0
let layout = collectionView!.collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = CGSize(width: cellWidth, height: cellHeight)
collectionView?.contentInset = UIEdgeInsets(top: insetY, left: insetX, bottom: insetY, right: insetX)
collectionView?.dataSource = self
collectionView?.delegate = self
}
}
extension InterestsViewController : UICollectionViewDataSource
{
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return interests.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InterestCell", for: indexPath) as! InterestCollectionViewCell
cell.interest = interests[indexPath.item]
return cell
}
}
extension InterestsViewController : UIScrollViewDelegate, UICollectionViewDelegate
{
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
{
let layout = self.collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
let cellWidthIncludingSpacing = layout.itemSize.width + layout.minimumLineSpacing
var offset = targetContentOffset.pointee
let index = (offset.x + scrollView.contentInset.left) / cellWidthIncludingSpacing
let roundedIndex = round(index)
offset = CGPoint(x: roundedIndex * cellWidthIncludingSpacing - scrollView.contentInset.left, y: -scrollView.contentInset.top)
targetContentOffset.pointee = offset
}
}
答案 0 :(得分:0)
你有多种选择可以做你想做的事。
例如,您可以在故事板中添加一个故事板引用到想要转换的控制器,或者视图控制器是否在同一个故事板中。然后按住Ctrl键并从视图拖动到要切换到的视图控制器(或故事板参考)。那里你将有不同的segues类型(显示,推送等)。 这是完成故事板的方式。
还有其他多种选择。但这可能是最简单的。
修改强>
您还可以从控制器创建8个segues(从控制器按住ctrl +拖动)到其他控制器或故事板参考。然后单击创建的segues,并在标识符检查器名称中单击每个具有唯一标识符的segue。
然后,当您单击单元格时,您可以确定单击了哪个单元格,并使用performSegueWithIdentifier(identifier, sender)