Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我们可以用swift制作2个动作的1个UIButton吗?_Ios_Swift_Select_Uibutton_Action - Fatal编程技术网

Ios 我们可以用swift制作2个动作的1个UIButton吗?

Ios 我们可以用swift制作2个动作的1个UIButton吗?,ios,swift,select,uibutton,action,Ios,Swift,Select,Uibutton,Action,我想为那样的按钮做两个动作。 为1按钮选择和取消选择的操作 @IBAction func btntouch(sender: UIButton) { if firsttouch { print bla bla change button to selected style. maybe background color. } else { } } 我该怎

我想为那样的按钮做两个动作。 为1按钮选择和取消选择的操作

   @IBAction func btntouch(sender: UIButton) {

         if firsttouch
        {
         print bla bla
         change button to selected style. maybe background color. 
         }
      else
        {

        }
}

我该怎么做?

创建2个
iActions

@IBAction func touchDown(_ sender: AnyObject) {
    print("down")
}

@IBAction func touchUp(_ sender: AnyObject) {
    print("up")
}

连接第一个时,确保
事件
设置为
着陆
。对于第二个按钮,请确保将其设置为
touchUpInside

如果您需要拆分两个按钮状态,如打开和关闭,请尝试以下操作:

var buttonSwitched : Bool = false

@IBAction func btntouch(sender: UIButton) {

    //this line toggle your button status variable
    //if true, it goes to false, and vice versa
    self.buttonSwitched = !self.buttonSwitched

    if self.buttonSwitched
    {
        //your UI styling
    }
    else
    {
        //your opposite UI styling
    }
}

是的,你可以。根据需要,可以将按钮的当前状态存储在视图控制器或模型中

如果第一次触摸导致的视觉变化需要在视图控制器的打开和关闭过程中保持,请将指示变化的值存储在模型中;如果需要在视图控制器显示时重置视觉效果,请将该值存储在视图控制器本身中:

var firstTouch = true
@IBAction func btntouch(sender: UIButton) {
     if firstTouch  {
         firstTouch = false
         ...
     } else {
         ...
     }
}

你的条件是什么?看看触地事件,但不确定你是否可以使用相同的方法。