How to call gesture tap on UIView programmatically in swift

后端 未结 23 1783
情歌与酒
情歌与酒 2020-11-28 18:50

I have a UIView and and I have added tap gesture to it:

let tap = UITapGestureRecognizer(target: self, action: Selector(\"handleTap:\"))
tap.delegate = self         


        
23条回答
  •  醉话见心
    2020-11-28 19:28

    STEP : 1

    @IBOutlet var viewTap: UIView!
    

    STEP : 2

    var tapGesture = UITapGestureRecognizer()
    

    STEP : 3

    override func viewDidLoad() {
        super.viewDidLoad()
        // TAP Gesture
        tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.myviewTapped(_:)))
        tapGesture.numberOfTapsRequired = 1
        tapGesture.numberOfTouchesRequired = 1
        viewTap.addGestureRecognizer(tapGesture)
        viewTap.isUserInteractionEnabled = true
    }
    

    STEP : 4

    func myviewTapped(_ sender: UITapGestureRecognizer) {
    
        if self.viewTap.backgroundColor == UIColor.yellow {
            self.viewTap.backgroundColor = UIColor.green
        }else{
            self.viewTap.backgroundColor = UIColor.yellow
        }
    }
    

    OUTPUT

提交回复
热议问题