Swift Preserve UISwitch State on UILongPress

2019-07-21 16:34发布

I added in my project UILongPressGestureRecognizer on the UISwitch

class ViewController: UIViewController,UIGestureRecognizerDelegate {

     var newSwitch:UISwitch!

func CreateSwitchWithIndex(index:Int) {
     newSwitch = UISwitch()
     newSwitch.tintColor = UIColor.blackColor()
     newSwitch.tag = index+1;
     newSwitch.addTarget(self, action: Selector("switchSen:"), forControlEvents: UIControlEvents.ValueChanged)
     self.view.addSubview(newSwitch)
     newSwitch.setOn(false, animated: true)

     let newSwitchConstraintL = NSLayoutConstraint(item: newSwitch, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50)

    let newSwitchConstraintH = NSLayoutConstraint(item: newSwitch, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50)

    let coordX = 368
    let coordY = 190 

    newSwitchConstraintX = NSLayoutConstraint(item: newSwitch, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 0, constant: CGFloat(coordX))

    newSwitchConstraintY = NSLayoutConstraint(item: newSwitch, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 0, constant: CGFloat(coordY))

    view.addConstraints([newSwitchConstraintX,newSwitchConstraintY,newSwitchConstraintH,newSwitchConstraintL])

    var longPress = UILongPressGestureRecognizer(target: self, action: "menu:")
     longPress.minimumPressDuration = 1
     newSwitch .addGestureRecognizer(longPress)
     longPress.delegate = self
 }

func switchSen(sender:UISwitch!) {
    if (sender.on == true){
        println("on")
    }
    else{
        println("off")
    }
}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {

    if(touch.view == newSwitch){
        return false
    }else{
        return true
    }
}
func menu(gesture:UILongPressGestureRecognizer) {

    if(gesture.state == UIGestureRecognizerState.Began){

        becomeFirstResponder()
        var menu = UIMenuController.sharedMenuController()
        var deleteItem = UIMenuItem(title: "Delete", action: Selector("delete"))
        menu.menuItems = [deleteItem]
        var lastLocation:CGPoint = gesture.locationInView(view)
        menu.setTargetRect(CGRectMake(lastLocation.x, lastLocation.y, 0.0, 0.0), inView: view)
        menu.setMenuVisible(true, animated: true)
    }
}

func delete() {
    println("Delete")

}

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}

func gestureRecognizer(UIGestureRecognizer,
shouldRecognizeSimultaneouslyWithGestureRecognizer:UIGestureRecognizer) -> Bool {
  return true
}

I managed to get the delete menu when I press on the switch itself but now... When I release the longpress my switch sets state the opposite of the initial state, so I think the shouldReceiveTouch don't go.

How can I preserve the status of my UISwitch ?

标签: swift ios8
0条回答
登录 后发表回答