Swift button.removeTarget for all action in View

2019-09-04 06:04发布

问题:

When I enable UIPanGestureRecognizer I would disable the action of the buttons pressed , and then when the gesture is being end I would like to re-enable the Action

func pan(rec:UIPanGestureRecognizer) {

var p:CGPoint = rec.locationInView(self.view)

var center:CGPoint = CGPointZero

switch rec.state {
case .Began:
    println("began")
    selectedView = view.hitTest(p, withEvent: nil)
    if selectedView != nil {
       self.view.bringSubviewToFront(selectedView!)
    }

case .Changed:
     if let buttonRecognize = rec.view as? UIButton {

button.removeTarget(self, action: Selector("\(title):"), forControlEvents:UIControlEvents.TouchUpInside) // but this disable all
case .Ended:
        println("ended")
        if let subview = selectedView {
            if let button = rec.view as? UIButton {
                if let title = button.titleForState(.Normal){
                    button.addTarget(self, action: Selector("\(title):"), forControlEvents: UIControlEvents.TouchUpInside)
                    selectedView = nil
                }}
    }    
case .Possible:
        println("possible")
    case .Cancelled:
        println("cancelled")
    case .Failed:
        println("failed")
    }}}
}

I tried in this way,and i have the reactivate of the action, but do not know how I can delete only the action of the button pressed ?

回答1:

You can set button.enabled = false instead of removing the button's target action.



标签: swift ios8