2016-08-29 45 views
0

我想为这样的按钮做2个动作。 选择并取消选择1个按钮的动作。我们可以用2个动作制作1个UIButton吗?

@IBAction func btntouch(sender: UIButton) { 

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

     } 
} 

我该怎么做?

+0

您的条件是什么? –

+0

看看touchDown事件,但不确定是否可以使用相同的方法。 – GoodSp33d

回答

1

在你需要分体式两种按钮状态的情况下 - 就像ON和OFF,试试这个:

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 
    } 
} 
+0

谢谢...工作正常。 – user3193248

1

创建2 IBActions

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

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

当连接第一个,确保event设置为touchDown。对于第二个,确保它被设置为touchUpInside

1

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

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

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