-1

私はImageViewをCollectionViewCellの中に持っています。画像をクリックすると、別のViewControllerが表示されます。どうすればいい?これはこれまでのコードです。CollectionViewCellの中にあるImageViewをクリックしてViewControllerに移動します

import UIKit 

class FirstViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 
    @IBOutlet weak var collectionView: UICollectionView! 

    var images = ["meal1", "photograph1", "meal1"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     self.collectionView.delegate = self 
     self.collectionView.dataSource = self 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return images.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionViewCell 

     //set images 
     cell.imageView.image = UIImage(named: images[indexPath.row]) 

     return cell 
    } 
} 

答えて

0

あなたはどちらか、私たちは、タイトルなしでUIButton and set the image property on it、またはあなたがUIGestureRecognizer to the UIImageViewを追加することができます。いずれにしても、アクションを受け取った後に表示したいViewControllerを表示または表示するだけです。

コールバック関数(buttonPressed:forCollectionCell:など)を持つCollectionCellDelegateプロトコルを作成し、CollectionViewに準拠させ、各セルのデリゲートをCollectionViewに設定できるようにします。次に、ボタン/イメージが押されたときにCollectionViewを呼び出すことができます。この場合は、新しいView Controllerを表示/プッシュする任意の動作をCollectionViewで処理できます。

1

あなたはこのコードを通過してくださいcollectionviewセルに画像タップを検出したい言ったように:

func connected(_ sender:AnyObject){ 
    print("you tap image number : \(sender.view.tag)") 
    //Your code for navigate to another viewcontroller 
    } 

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.connected(_:))) 

    cell.yourImageView.isUserInteractionEnabled = true 
    cell.yourImageView.tag = indexPath.row 
    cell.yourImageView.addGestureRecognizer(tapGestureRecognizer) 

そして、あなたのViewControllerに法の下に追加 - ことを確認してくださいあなたのセルイメージのユーザー入力は有効です

0

collectionGiの画像ビューにtabGestureRecognizerを追加しますew "cellForItemAt"メソッドを呼び出し、レコグナイザのメソッドでsegueを呼び出して、目的のviewcontrollerに移動します。