2017-05-05 8 views
0

私は、多くのimageViewsを持つ水平スクロール可能なScrollViewを持っています。ユーザーがクリックした画像を強調表示したいのですが、これを行う方法がわかりません。私は、画像にタップジェスチャーを追加しました:scrollViewを繰り返して、クリックされた画像を見つけてください。

let tap = UITapGestureRecognizer(target: self, action: #selector(imgTapped(_:))) 

しかし - 私は、ここからimgTapped機能で何をするかUREない...私は、各ImageViewのための固有のタグを持っています。

ご協力いただければ幸いです。 、

func imgTapped(_ sender: UIGestureRecognizer) { 
    // get the tag for the clicked imageView 
    guard let tag = sender.view?.tag else { return } 

    let imageView = ImageViews.filter { $0.tag == tag }.first 
} 

は、そうでなければ、あなたのscrollViews subviewsを反復処理することができますコメントをチェックアウトし、コードの中で何が起こるか見て:あなたはすべてのコレクションを持っている場合

答えて

0

imageViewsあなたはこのような何かを行うことができ

func imgTapped(_ sender: UIGestureRecognizer) { 
    // get the tag for the clicked imageView 
    guard let tag = sender.view?.tag else { return } 

    // iterate through your scrollViews subviews 
    // and check if it´s an imageView 
    for case let imageView as UIImageView in self.imageScrollView.subviews { 
     // check if the tag matches the clicked tag 
     if imageView.tag == tag { 
      // this is the tag the user has clicked on 
      // highlight it here 
     } 
    } 
} 
関連する問題