2017-07-12 14 views
0

テーブルセルのUIImageViewにジェスチャ認識機能を追加するにはどうすればよいですか?ユーザーがセル内の画像をタップすると、画像が変わり、データモデルが更新されるようにします。テーブルセル内の画像ビューにジェスチャ認識機能を追加する

これはUITableViewControllerに設定する必要があります。私のコードは現在、セルの任意の場所をタップしている場合にコマンドを実行できますが、画像がタップされている場合にのみ実行されます。

viewDidLoad

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Load sample data 
    loadSampleHabits() 

    // Initialize tap gesture recognizer 
    var recognizer = UITapGestureRecognizer(target: self, action: #selector(tapEdit(recognizer:))) 
    // Add gesture recognizer to the view 
    self.tableView.addGestureRecognizer(recognizer) 

ジェスチャ認識、最大Iセットアップそして、これは機能二疑問として

//action method for gesture recognizer 
func tapEdit(recognizer: UITapGestureRecognizer) { 
    if recognizer.state == UIGestureRecognizerState.ended { 
     let tapLocation = recognizer.location(in: self.tableView) 
     if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) { 
      if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) as? HabitTableViewCell { 
       print("Row Selected") 

      } 
     } 
    } 

What the cell looks like

である私が追加したい場合は、任意の競合がありますセルへのジェスチャ認識子とセル内の画像ビュー

+0

あなたの方法からもしrecognizer.state == UIGestureRecognizerState.ended条件を削除してくださいすることができます。私のansをチェックしてください。 – luckyShubhra

+0

@ b.gibson:私はluckyShubhraの答えに適応します。私は自分のプロジェクトのどこかで同じものを使っていましたが、問題は発生しませんでした。 –

+0

イメージビューで 'UITapGestureRecognizer'を使用する場合、' state'をチェックする必要はありません。これは実際には連続したジェスチャー(長押しやパンジェスチャーなど)にのみ関係します。しかし、単に[iOS Dev suggest](https://stackoverflow.com/a/45049086/1271826)のような画像を持つボタンを使用すると、私にとってもっと意味があります。 – Rob

答えて

3

はあなたが必要に応じてあなたのテーブルビューの代わりに、ImageViewの上、ジェスチャー認識を追加しているインデックスパス

 var recognizer = UITapGestureRecognizer(target: self, action: #selector(tapEdit(recognizer:))) 
     // Add gesture recognizer to the view 
     cell.yourimageviewname.addGestureRecognizer(recognizer) 

cell.yourimageviewname.userInteractionEnabled = true; 
+1

change cell.youtimageviewname.isUserInteractionEnabled = true –

4

での行のセルにこの行を追加します。あなたのコードをviewDidLoadからcellForRowAtIndexPathに移動し、セルを設定中に各セルのimageViewにジェスチャーを追加する必要があります。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
    var recognizer = UITapGestureRecognizer(target: self, action: #selector(tapEdit(recognizer:))) 
    // Add gesture recognizer to your image view 
    cell.yourimageview.addGestureRecognizer(recognizer) 
} 

注:それはジェスチャーとdidselectにおける紛争の少ないチャンスを持っていると私はUILongPressGestureRecognizerを使用することをお勧めしますあなたの条件のために、あなたのイメージ図のuserinteraction

cell.yourimageview.userInteractionEnabled = YES; 

を有効にしてくださいください。 YoはviewDidLoadにUILongPressGestureRecognizerを追加し、要件に応じてアクセスできます。

let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.handleLongPress(_:))) 
lpgr.minimumPressDuration = 1 
tableView.addGestureRecognizer(lpgr) 

あなたの方法からif recognizer.state == UIGestureRecognizerState.ended条件を削除しようとすることができ

func handleLongPress(_ gesture: UILongPressGestureRecognizer){ 
if gesture.state != .began { return } 
let tapLocation = gesture.location(in: self.tableView) 
    if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) { 
     if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) as? HabitTableViewCell { 
      print("Row Selected") 

     } 
} 

の方法を定義します。

UITapGestureRecognizerは離散ジェスチャであるため、ジェスチャが認識されたときにイベントハンドラが1回だけ呼び出されます。あなたは状態をまったく確認する必要はありません。確かにあなたは.Beganの状態のための電話を受けません。詳細については、@Rob ans hereをご検討ください。私の提案については

2

あなたは、パフォーマンス 改善のために、細胞内の

UIButtons

特別にこのために設計されており、広範囲に触れるために、アップルによって最適化されていますがUIButtonを使用する必要があります。

画像をセルに入れたい場合は、UIButtonの画像を内部に使用できます。

0

私はこのようなソリューションを設計しました。私は、以下のサンプルコードを記述します。

import UIKit 

protocol CellImageTapDelegate { 
    func tableCell(didClickedImageOf tableCell: UITableViewCell) 
} 

class SampleCell : UITableViewCell { 

    var delegate : CellImageTapDelegate? 
    var tapGestureRecognizer = UITapGestureRecognizer() 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     initialize() 
    } 

    private func initialize() { 
     tapGestureRecognizer.addTarget(self, action: #selector(SampleCell.imageTapped(gestureRecgonizer:))) 
     self.addGestureRecognizer(tapGestureRecognizer) 
    } 

    func imageTapped(gestureRecgonizer: UITapGestureRecognizer) { 
     delegate?.tableCell(didClickedImageOf: self) 
    } 
} 

class ViewController: UITableViewController, CellImageTapDelegate { 

    // CellImageTapDelegate 
    func tableCell(didClickedImageOf tableCell: UITableViewCell) { 
     if let rowIndexPath = tableView.indexPath(for: tableCell) { 
      print("Row Selected of indexPath: \(rowIndexPath)") 
     } 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 10 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "SampleCellID", for: indexPath) as! SampleCell 
     cell.delegate = self 
     return cell 
    } 
} 

は、ストーリーボード 1に次の操作を行うことを忘れないでくださいtableviewcell
のtableviewcell 2. set class of tableviewcell 3セットの再利用識別子の2 1. enable user interaction of imageview ImageViewのセットクラスのユーザインタラクションを可能にします3. set reuse identifier of tableviewcell

+0

このソリューションのテスト済みプロジェクト。 [googleドライブのzipプロジェクト](https://drive.google.com/open?id=0B70RpDc-VWUuSVA0NEFnRldfSE0) – Codus

1
// create an instance of UITapGestureRecognizer and tell it to run 
// an action we'll call "handleTap:" 
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:")) 
// we use our delegate 
tap.delegate = self 
// allow for user interaction 
cell.imageViewName.userInteractionEnabled = true 
// add tap as a gestureRecognizer to tapView 
cell.imageViewName.addGestureRecognizer(tap) 
関連する問題