Pass Parameter with UITapGestureRecognizer

后端 未结 3 871
Happy的楠姐
Happy的楠姐 2021-02-18 15:51

Is there any way i can pass parameters with UITapGestureRecognizer? I\'ve seen this answered for objective-c but couldn\'t find an answer for swift

test.userInte         


        
3条回答
  •  温柔的废话
    2021-02-18 16:37

    For Swift 4

    In Viewdidload

    let label     =   UILabel(frame: CGRect(x: 0, y: h, width: Int(self.phoneNumberView.bounds.width), height: 30))
                    label.textColor = primaryColor
                    label.numberOfLines = 0
                    label.font = title3Font
                    label.lineBreakMode = .byWordWrapping
                    label.attributedText = fullString
    
     let phoneCall = MyTapGesture(target: self, action: #selector(self.openCall))
            phoneCall.phoneNumber = "\(res)"
                label.isUserInteractionEnabled = true
                label.addGestureRecognizer(phoneCall)
    

    Function as

    @objc func openCall(sender : MyTapGesture) {
            let number = sender.phoneNumber
            print(number)
    }
    

    Write Class as

    class MyTapGesture: UITapGestureRecognizer {
        var phoneNumber = String()
    }
    

    Follow Step Properly and make change according to your variable , button ,label . It WORKS PROPERLY

提交回复
热议问题