У меня огромные трудности с моим кодом. Я разрабатываю приложение, которое показывает тексты песен и аккорды песен. Я разделил аккорды и тексты песен, используя два наложенных друг на друга текстовых представления.
Проблема, которую я получил в этом проекте, заключается в функции изменения высоты тона. Я пытаюсь объяснить меня как можно лучше:
Всего аккордов 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
}
Если вы запустите этот код, вы увидите, что преобразование высоты тона не работает должным образом.
Надеюсь, я был достаточно ясен ....