0

I'm trying to improve my swift coding.

I have a fully working if-else nested function which I believe could be better optimised with a Switch Statement. Can anybody advise the correct way to do this code-refactoring ?

Here's my working code:

    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()}
                        }

    }
3
  • 1
    Working code should be posted on Code Review Commented Oct 14, 2019 at 8:50
  • What's the type of self.placeholderText1? Commented Oct 14, 2019 at 9:27
  • placeholderTextn are labels Commented Oct 14, 2019 at 9:29

2 Answers 2

2

That is not optimized code, but like follow code, combine duplicate action code as function

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)
Sign up to request clarification or add additional context in comments.

8 Comments

In findButton you can use first function on button array so no need to write again if_else there.
findButton won't compile. You don't handle the case where no button matches.
@PravinTate Like this? [firstButton, secondButton, thirdButton, fourthButton, fifthButton].first{ $0.frame.contains(location) }
@JeremyP Perfect. As question on if_else so now we make it more optimised. :D
@InKwonJamesKim guard needs an else. I fixed it for you.
|
2

You can rewrite your code to,

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

In the above code,

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

Replace UITextInput with the type of placeholderText1.

2 Comments

if let button = [UIView(), UIView(), UIView(), UIView()].filter({ - Here you can use first function for more optimised.
I'm saying instead of using filter func you can use first func there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.