2017-04-20 122 views
1
敲擊手勢識別分配給圖像的滾動視圖內

我試圖創建的圖像和可點擊蘋果商店一樣的屏幕截圖, 當用戶觸摸上一個圖像的一些行動電話的滾動視圖!但在這裏我無法找到這個問題,這是行不通的! 請注意,我使用的所有UI都是由代碼完成的!在斯威夫特

func initScreenshots(){ 
    if screenShotsView.subviews.count > 0{ 
     screenShotsView.subviews.forEach({ $0.removeFromSuperview() }) 
    } 

    imageArray.append(UIImage.init(named: "1.jpeg")!) 
    imageArray.append(UIImage.init(named: "2.jpeg")!) 

    for image in imageArray { 
     let index = imageArray.index(of: image) 
     let imageView:UIImageView = { 
      let iv = UIImageView() 
      iv.image = image 
      iv.translatesAutoresizingMaskIntoConstraints = false 
      iv.contentMode = .scaleAspectFit 
      return iv 
     }() 
     imageView.isUserInteractionEnabled = true 
     imageView.addGestureRecognizer(tapGesture) 
     screenShotsView.addSubview(imageView) 
     imageView.topAnchor.constraint(equalTo: screenShotsView.topAnchor, constant: 10).isActive = true 
     imageView.bottomAnchor.constraint(equalTo: screenShotsView.bottomAnchor, constant: 10).isActive = true 
     imageView.heightAnchor.constraint(equalToConstant: 350).isActive = true 
     imageView.widthAnchor.constraint(equalToConstant: 190).isActive = true 
     if index == 0 { 
      imageView.leftAnchor.constraint(equalTo: screenShotsView.leftAnchor, constant: 10).isActive = true     
     }else if(index == imageArray.count - 1){ 
      imageView.rightAnchor.constraint(equalTo: screenShotsView.rightAnchor, constant: 10).isActive = true 
      imageView.leftAnchor.constraint(equalTo: ((screenShotsView.subviews[index! - 1]).rightAnchor), constant: 10).isActive = true 
     }else{ 
      imageView.leftAnchor.constraint(equalTo: ((screenShotsView.subviews[index! - 1]).rightAnchor), constant: 10).isActive = true 
     } 
     imageView.isUserInteractionEnabled = true 
     imageView.addGestureRecognizer(tapGesture) 
    } 

} 

而且你可以看到我的手勢識別:

let tapGesture:UITapGestureRecognizer = { 
    let gr = UITapGestureRecognizer(target: self, action: #selector(tapBlurButton(_:))) 
    gr.numberOfTapsRequired = 1 
    return gr 
}() 

我的回調函數是簡單的這樣!但它不工作:(

func tapBlurButton(_ sender: UITapGestureRecognizer) { 
    print("Please Help!") 
} 
+0

您是否嘗試過使用按鈕並設置按鈕的背景圖像與你想要的? –

+0

@OrenEdrich不,但我有另一個按鈕,在我看來,我已經添加了這個手勢識別器,但仍然不工作:( – shaibow

+1

你不需要添加一個手勢識別器到一個按鈕將它連接到你的.swift文件。或者讓一個if(你的按鈕).isSelected = true {你的代碼} –

回答

2

imageView需要自己UITapGestureRecognizer。你不能只是建立一個和多次使用它。

移動UITapGestureRecognizer的創建到您的循環,它將工作