Замена текста для изменения высоты звука / ключа - PullRequest
1 голос
/ 14 апреля 2020

У меня огромные трудности с моим кодом. Я разрабатываю приложение, которое показывает тексты песен и аккорды песен. Я разделил аккорды и тексты песен, используя два наложенных друг на друга текстовых представления.

Проблема, которую я получил в этом проекте, заключается в функции изменения высоты тона. Я пытаюсь объяснить меня как можно лучше:

Всего аккордов 12: Do-Do # -Re-Re # -Mi-Fa-Fa # -Sol-Sol # -La-La # -Si

Я использую две кнопки + и - для изменения высоты тона

Для того, чтобы содержать пробелы от аккорда к другому, я использовал replacingOccurrences(of:with:options:) метод экземпляра следующим образом:

//MARK: - Change pitch ?
    @IBAction func risePitch(_ sender: UIButton) {

                //positive side was pressed
                let dosre = chords.text.replacingOccurrences(of: "Do#", with: "Re", options: .widthInsensitive)
                chords.text = dosre

                let dodos = chords.text.replacingOccurrences(of: "Do", with: "Do#", options: .widthInsensitive)
                chords.text = dodos

                let resmi = chords.text.replacingOccurrences(of: "Re#", with: "Mi", options: .widthInsensitive)
                chords.text = resmi

                let remi = chords.text.replacingOccurrences(of: "Re", with: "Re#", options: .widthInsensitive)
                chords.text = remi

                let fassol = chords.text.replacingOccurrences(of: "Fa#", with: "Sol", options: .widthInsensitive)
                chords.text = fassol

                let mifa = chords.text.replacingOccurrences(of: "Mi", with: "Fa", options: .widthInsensitive)
                chords.text = mifa

                let fafas = chords.text.replacingOccurrences(of: "Fa", with: "Fa#", options: .widthInsensitive)
                chords.text = fafas

                let solsla = chords.text.replacingOccurrences(of: "Sol#", with: "La", options: .widthInsensitive)
                chords.text = solsla

                let solsols = chords.text.replacingOccurrences(of: "Sol", with: "Sol#", options: .widthInsensitive)
                chords.text = solsols

                let lassi = chords.text.replacingOccurrences(of: "La#", with: "Si", options: .widthInsensitive)
                chords.text = lassi

                let lalas = chords.text.replacingOccurrences(of: "La", with: "La#", options: .widthInsensitive)
                chords.text = lalas

                let sido = chords.text.replacingOccurrences(of: "Si", with: "Do", options: .widthInsensitive)
                chords.text = sido

    }


    @IBAction func decreasePitch(_ sender: UIButton) {
        //negative side was pressed
        let dosre = chords.text.replacingOccurrences(of: "Do#", with: "Do", options: .widthInsensitive)
        chords.text = dosre

        let dore = chords.text.replacingOccurrences(of: "Do", with: "Si", options: .widthInsensitive)
        chords.text = dore

        let resmi = chords.text.replacingOccurrences(of: "Re#", with: "Re", options: .widthInsensitive)
        chords.text = resmi

        let remi = chords.text.replacingOccurrences(of: "Re", with: "Do#", options: .widthInsensitive)
        chords.text = remi

        let mifa = chords.text.replacingOccurrences(of: "Mi", with: "Re#", options: .widthInsensitive)
        chords.text = mifa

        let fassol = chords.text.replacingOccurrences(of: "Fa#", with: "Fa", options: .widthInsensitive)
        chords.text = fassol

        let fafas = chords.text.replacingOccurrences(of: "Fa", with: "Mi", options: .widthInsensitive)
        chords.text = fafas

        let solsla = chords.text.replacingOccurrences(of: "Sol#", with: "Sol", options: .widthInsensitive)
        chords.text = solsla

        let solsols = chords.text.replacingOccurrences(of: "Sol", with: "Fa#", options: .widthInsensitive)
        chords.text = solsols

        let lassi = chords.text.replacingOccurrences(of: "La#", with: "La", options: .widthInsensitive)
        chords.text = lassi

        let lalas = chords.text.replacingOccurrences(of: "La", with: "Sol#", options: .widthInsensitive)
        chords.text = lalas

        let sido = chords.text.replacingOccurrences(of: "Si", with: "La#", options: .widthInsensitive)
        chords.text = sido


    }

Если вы запустите этот код, вы увидите, что преобразование высоты тона не работает должным образом.

Надеюсь, я был достаточно ясен ....

1 Ответ

1 голос
/ 15 апреля 2020

Будет намного проще, если вы разделите ноты на массив строк (и у вас будет временный массив для внесения изменений), а затем будете иметь функции повышения и понижения высоты тона +1 или -1 для каждой ноты каждый время. Затем вы можете свернуть массив аккордов в строку, чтобы отобразить его. Этот код здесь работает:

var masterChords = ["Do",  "Do#",  "Re",  "Re#",  "Mi",  "Fa",  "Fa#",  "Sol",  "Sol#",  "La",  "La#",  "Si"]
var chords = ["Do", "Sol", "Mi"]

func raisePitch() {
    for i in 0...chords.count - 1 {
        for j in 0...masterChords.count - 1 {
            if chords[i] == masterChords[j] {
                if j < masterChords.count - 1 {
                    chords[i] = masterChords[j + 1]
                    break
                } else {
                    chords[i] = masterChords[0]
                    break
                }
            }
        }
    }
}

func lowerPitch() {
    for i in 0...chords.count - 1 {
        for j in 0...masterChords.count - 1 {
            if chords[i] == masterChords[j] {
                if j > 0 {
                    chords[i] = masterChords[j - 1]
                    break
                } else {
                    chords[i] = masterChords[masterChords.count - 1]
                    break
                }
            }
        }
    }
}


//Use the code below to test
print(chords)

raisePitch()

print(chords)

lowerPitch()

print(chords)

lowerPitch()

print(chords)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...