我可以使用} else {不使用" if"声明?

时间:2017-08-22 14:58:02

标签: swift

我尝试动画应用中的某些按钮,当您点击较大的按钮时,会出现三个较小的按钮,但较大的按钮不会更改它的状态,如果它确实我会把它设置为基本上

如果较大的一个改变状态/图像(打开或关闭) 然后小的一个出来了。

问题在于我希望较小的一个回来。我不能使用else语句来设置较小的按钮位置。

编辑:

@IBAction func rainBtnPressed(_ sender: UIButton) {

    UIView.animate(withDuration: 0.3, animations: {
        self.heavyThunderBtn.center = self.heavyThunderCenter
        self.lightRainBtn.center = self.lightRainCenter
        self.rainOnRoofBtn.center = self.rainOnRoofCenter

    })
} else {
    UIView.animate(withDuration: 0.3, animations: {
    heavyThunderBtn.center = rainBtn.center
    lightRainBtn.center = rainBtn.center
    rainOnRoofBtn.center = rainBtn.center

})

}

1 个答案:

答案 0 :(得分:2)

不,但是......

否定您的情况

您可以轻松转换此代码块

if condition {
    print("Condition is true")
} else {
    print("Condition is false")
}

到这个

if !condition {
    print("Condition is false")
}

护卫

或者您可以使用guard构造,但是您需要打破 else内的流程。

guard condition else {
    print("Condition is false")
    return
}