簡體   English   中英

手勢識別器未觸發iOS

[英]Gesture Recognizer not triggered iOS

我在滾動視圖上有圖像視圖。 我在圖像視圖上添加了多個子視圖。

- > UIScrollView中

- >的UIImageView

---> UIViews

我想將手勢識別器添加到子視圖中。 但這行不通。 我的錯誤在哪里? 函數handleTap()未觸發。

 func addSubViewOnImageView(mPoint:CGPoint, mSize: CGSize){
    let rect = CGRect(origin: mPoint, size: mSize)
    let sView = UIView(frame: rect)
    sView.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self,  action: #selector(self.handleTap(_:)))
    tap.delegate = self
    sView.addGestureRecognizer(tap)
    imageView.addSubview(sView)
  }

 @objc func handleTap(_ sender: UITapGestureRecognizer) {
        print("tapped any sub view")
  }

此屬性是從UIView父類繼承的。 此類將此屬性的默認值更改為NO。

為您的imageview啟用用戶交互,默認情況下為false,有關更多信息,請從蘋果文檔中獲取信息

imageView.isUserInteractionEnabled = true

只是檢查

yourImageView.isUserInteractionEnabled = true 

有沒有?

如果您不想執行yourImageView.isUserInteractionEnabled = true

只需使您的UIView class a subclass of UIControl from storyboard請參見屏幕截圖。

它將充當UIButton而無需編寫用於添加點擊手勢的代碼, just add an action and like通常我們對UIImageView所做的just add an action and like

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM