簡體   English   中英

UITapGestureRecognizer操作不起作用

[英]UITapGestureRecognizer action don't work

我的UITapGestureRecognizer有問題。 我使用此代碼:

let gesture = UITapGestureRecognizer(target: self, action: #selector(self.SelectPictureAction(_:)))
    cardView.addGestureRecognizer(gesture)

和我的功能在這里:

@objc func SelectPictureAction(_ sender : UITapGestureRecognizer) {
    print("card moment is Tapped")
}

問題是我點擊我的卡后什么也沒發生。 我認為問題是我在不是同一類的init的UIView中安裝了一個手勢...

所以我的問題是:

當我在另一個類中調用的UIView中添加gesture時,可能會出現麻煩嗎?


這是我使用的3類的代碼:

TableView代碼:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


@IBOutlet var tableView: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register(CardCell.self, forCellReuseIdentifier: "Cell")
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier:"Cell") as! CardCell
    cell.widthOfDevice = tableView.bounds.size.width
    if indexPath.row == 1 {
        cell.doTheWork(statutOfCard: .selectMoment)
    }else{
        cell.doTheWork(statutOfCard: .goMoment(picture:#imageLiteral(resourceName: "img2")))
    }

    return cell
}
}

CardCell代碼:

class CardCell : UITableViewCell {
let cardView = UIView()
enum statut {
    case selectMoment
    case goMoment(picture: UIImage)
}

func doTheWork(statutOfCard : statut){
    switch statutOfCard {
    case .selectMoment:
        self.drawMomentPicture()
    case .goMoment(let picture):
        self.drawGoMoment(image: picture)
    default:
        cardViewHeightCon.constant = 300
        self.addCardShadow()
    }
}

func drawMomentPicture(){
    if(cardIsDraw == false){
        widthOfCard = widthOfDevice - (widthMarginConstraint*2)
        cardViewHeightCon.constant = widthOfCard!*ratioOfImage
        _ = SelectMoment(countUserMoment: 1, cardView: self.cardView, tableViewCell: self) // Here I call my class
        self.addCardShadow()
        cardIsDraw = true
    }
}
}

SelectMoment代碼:

class SelectMoment {
    let countUserMoment: Int
    let cardView : UIView
    let selfTableViewCell : UITableViewCell

init(countUserMoment: Int, cardView : UIView, tableViewCell : UITableViewCell) {
    self.countUserMoment = countUserMoment
    self.cardView = cardView
    self.selfTableViewCell = tableViewCell
    self.drawCard()
}
func drawCard(){
    cardView.backgroundColor = .yellow
    cardView.isUserInteractionEnabled = true
    let gesture = UITapGestureRecognizer(target: self, action: #selector(self.SelectMomentAction(_:)))
    cardView.addGestureRecognizer(gesture)
}
@objc func SelectMomentAction(_ sender : UITapGestureRecognizer) {
    print("card moment is Tapped")
}
}

您的選擇器參數似乎是錯誤的,請像下面這樣使用。

let gesture = UITapGestureRecognizer(target: self, action: #selector(self.SelectMomentAction(_:)))

@objc func SelectMomentAction(_ sender : UITapGestureRecognizer) {
    print("card moment is Tapped")
}

希望這行得通。

您想要的可能類似於幫助器結構:

struct SelectMoment {
    // ...
}
class CardCell : UITableViewCell {
    var selectMoment : SelectMoment!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.selectMoment = SelectMoment(...)
        // ...
    }
    // ...
}

現在,您的SelectMoment結構實例可以與CardCell實例保持聯系,並且CardCell可以看到它。 擁有正確的屬性,他們甚至可以毫無問題地相互交流。

請注意,該架構只是向您展示如何分離兩種類型的功能的草圖。 我不知道您打算使用的用例。 也許您想要的是能夠為每個不同的單元格類型分配不同配置的SelectMoment。 在這種情況下,請不要在viewDidLoad設置selectMoment 允許其他一些類對其進行設置(可能通過初始化程序,也可能只是直接對其進行設置),並做出相應的響應。 這種架構稱為依賴注入

暫無
暫無

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

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