在Swift中使用switch语句重构IF-Else嵌套代码

问题描述 投票:0回答:1

我正在尝试改善我的快速编码。

我有一个可以正常工作的if-else嵌套函数,我相信可以使用Switch语句更好地对其进行优化。任何人都可以建议正确的方法来执行此代码重构吗?

这是我的工作代码:

    if firstButton.frame.contains(location) {
        UIView.animate(withDuration: 0.4) {
            self.firstButton.transform = CGAffineTransform(scaleX: -1, y: 1)
            self.firstButton.alpha = 0
                 if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.tapCount += 1}
            else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.tapCount += 1}
            else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.tapCount += 1}
            else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.tapCount += 1}
            else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.lastLetterTapped()}
        }
    } else
        if secondButton.frame.contains(location) {
            UIView.animate(withDuration: 0.4) {
                self.secondButton.transform = CGAffineTransform(scaleX: -1, y: 1)
                self.secondButton.alpha = 0
                     if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.tapCount += 1}
                else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.tapCount += 1}
                else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.tapCount += 1}
                else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.tapCount += 1}
                else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.lastLetterTapped()}
            }
    } else
            if thirdButton.frame.contains(location) {
                UIView.animate(withDuration: 0.4) {
                    self.thirdButton.transform = CGAffineTransform(scaleX: -1, y: 1)
                    self.thirdButton.alpha = 0
                         if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.tapCount += 1}
                    else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.tapCount += 1}
                    else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.tapCount += 1}
                    else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.tapCount += 1}
                    else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.lastLetterTapped()}
                }
            } else
                if fourthButton.frame.contains(location) {
                    UIView.animate(withDuration: 0.4) {
                        self.fourthButton.transform = CGAffineTransform(scaleX: -1, y: 1)
                        self.fourthButton.alpha = 0
                             if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.tapCount += 1}
                        else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.tapCount += 1}
                        else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.tapCount += 1}
                        else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.tapCount += 1}
                        else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.lastLetterTapped()}
                    }
                } else
                    if fifthButton.frame.contains(location) {
                        UIView.animate(withDuration: 0.4) {
                            self.fifthButton.transform = CGAffineTransform(scaleX: -1, y: 1)
                            self.fifthButton.alpha = 0
                                 if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.tapCount += 1}
                            else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.tapCount += 1}
                            else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.tapCount += 1}
                            else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.tapCount += 1}
                            else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.lastLetterTapped()}
                        }

    }
swift if-statement nested switch-statement refactoring
1个回答
0
投票

不是经过优化的代码,但是像跟随代码一样,将重复的动作代码作为函数组合


func findButton(location: CGPoint) -> UIButton {
    if firstButton.frame.contains(location) {
        return firstButton
    } else if secondButton.frame.contains(location) {
        return secondButton
    } else if thirdButton.frame.contains(location) {
        return thirdButton
    } else if fourthButton.frame.contains(location) {
        return fourthButton
    } else if fifthButton.frame.contains(location) {
        return fifthButton
    }
}

func anything (button: UIButton) {
    UIView.animate(withDuration: 0.4) {[weak self] in
    guard let `self` = self { return }

    self.button.transform = CGAffineTransform(scaleX: -1, y: 1)
    self.button.alpha = 0

    switch self.tapCount {
      case 0: 
        self.placeholderText1.textWithAnimation(text: button.text!, duration: 0.3)
        self.tapCount += 1
      case 1: 
        self.placeholderText2.textWithAnimation(text: button.text!, duration: 0.3)
        self.tapCount += 1
      case 2: 
        self.placeholderText3.textWithAnimation(text: button.text!, duration: 0.3)
        self.tapCount += 1
      case 3: 
        self.placeholderText4.textWithAnimation(text: button.text!, duration: 0.3)
        self.tapCount += 1
      case 4: 
        self.placeholderText5.textWithAnimation(text: button.text!, duration: 0.3)
        self.lastLetterTapped()
    }
}

anything(button: findButton(location location)) 
© www.soinside.com 2019 - 2024. All rights reserved.