@selector() in Swift?

后端 未结 23 2947
清酒与你
清酒与你 2020-11-21 15:24

I\'m trying to create an NSTimer in Swift but I\'m having some trouble.

NSTimer(timeInterval: 1, target: self, selector: test(), us         


        
23条回答
  •  滥情空心
    2020-11-21 16:04

    Since Swift 3.0 is published, it is even a little bit more subtle to declare a targetAction appropriate

    class MyCustomView : UIView {
    
        func addTapGestureRecognizer() {
    
            // the "_" is important
            let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MyCustomView.handleTapGesture(_:)))
            tapGestureRecognizer.numberOfTapsRequired = 1
            addGestureRecognizer(tapGestureRecognizer)
        }
    
        // since Swift 3.0 this "_" in the method implementation is very important to 
        // let the selector understand the targetAction
        func handleTapGesture(_ tapGesture : UITapGestureRecognizer) {
    
            if tapGesture.state == .ended {
                print("TapGesture detected")
            }
        }
    }
    

提交回复
热议问题