Рефакторинг IF-Else вложенного кода с оператором switch в Swift - PullRequest
0 голосов
/ 14 октября 2019

Я пытаюсь улучшить свое быстрое кодирование.

У меня есть полностью работающая вложенная функция 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()}
                        }

    }

Ответы [ 2 ]

2 голосов
/ 14 октября 2019

Вы можете переписать свой код на

if let locationView = [firstButton, secondButton, thirdButton, fourthButton].first(where: {$0.frame.contains(location)}) {
    UIView.animate(withDuration: 0.4) {[weak self] in
        guard let `self` = self else {
            return
        }

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

        var placeholderText: UITextInput? //change the type to whatever type of placeholderText1 and others
        switch self.tapCount {
        case 0:
            placeholderText = self.placeholderText1
        case 1:
            placeholderText = self.placeholderText2
        case 2:
            placeholderText = self.placeholderText3
        case 3:
            placeholderText = self.placeholderText4
        case 4:
            placeholderText = self.placeholderText5
            self.lastLetterTapped()
        default:
            break
        }
        if let text = placeholderText,  {
            text.textWithAnimation(text: button.text, duration: 0.3)
            if self.tapCount != 4 {
                self.tapCount += 1
            }
        }
    }
}

. В приведенном выше коде

var placeholderText: UITextInput? //change the type to whatever type of placeholderText1 and others

Заменить UITextInput на тип placeholderText1.

2 голосов
/ 14 октября 2019

Это не оптимизированный код, но, как и в случае с кодом, объедините повторяющийся код действия как функцию

func anything (button: UIButton) {
    UIView.animate(withDuration: 0.4) {[weak self] in
    guard let `self` = self else { 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()
    }
}

// 
guard let foundButton = [
  firstButton, secondButton, thirdButton, fourthButton, fifthButton
].first{ $0.frame.contains(location) } else { return }

anything(button: foundButton)
...