Post

Replies

Boosts

Views

Activity

Passing a variable to a function
I don’t know if I am putting this question in the right place. I am not a developer but am coding in swift / SwiftUI more as a hobby, on an iPad using Swift Playgrounds. I am coding a basic drum machine and trying to get start stop buttons functioning. My loops are in functions. I have a start and stop button. The start button runs the drum loops. The drum loops are infinite so when I want to stop I want to pass a Boolean (true/false) variable from the stop button to interact with the while loop in the function. However once I have tapped the start button and the loop starts running, the stop button is unavaillable - it can’t be tapped. The start button has a pressed in appearance and also can’t be tapped. if any one wanted to look at the code I could paste it here, or an abbreviated version to show the relevant part of my code.
3
0
430
Jun ’21
Passing a variable to a function
I am reposting this question with my code included. I don’t know if I am putting this question in the right place. I am not a developer but am coding in swift / SwiftUI more as a hobby, on an iPad using Swift Playgrounds. I am coding a basic drum machine and trying to get start stop buttons functioning. My loops are in functions. I tap the start button and the loop starts playing as expected. Then I was hoping to set my Boolean variable - “playing” to false with the stop button, and stop the while loop in my function called loop1. However the stop button can’t be tapped so I can change the variable. The start button takes on a pressed appearance and can’t be tapped as either. Below is a shorter version of my code. import SwiftUI import PlaygroundSupport import AVFoundation PlaygroundPage.current.wantsFullScreenLiveView = true var playing:Bool = true var t2: Double = 1 var t1: Double = 1 var BPM:Double = 100 let screenWidth  = UIScreen.main.bounds.width let screenHeight = UIScreen.main.bounds.height struct DrumMachine: View{          @State var SnarePlayer: AVAudioPlayer?     var body: some View{         Button(action: {// start button                          loop1()         }) {             Image(systemName: "play.circle")                 .foregroundColor(.green)         }         .frame(width: 0.2screenHeight, height: 0.2screenHeight)         .scaleEffect(5)         .background(Color.clear)         //finish of play button         Button(action: {// stop button             playing = false         }) {             Image(systemName: "stop.circle")                 .foregroundColor(.red)                 .frame(width: 0.2screenHeight, height: 0.2screenHeight)                 .scaleEffect(5)                 .background(Color.clear)                 .disabled(false)         }         .onAppear(){Snare(vol: 0)}     }//end of some view               func Snare(vol: Float){         if let  SnareURL = Bundle.main.url(forResource: "Snare Drum", withExtension: "mp3") {             do {                 try self.SnarePlayer = AVAudioPlayer(contentsOf: SnareURL) /// make the audio player                 SnarePlayer?.volume = vol                                  self.SnarePlayer?.play() /// start playing                              } catch {                 print("Couldn't play audio. Error: (error)")             }                      }     }//end of snare function          func loop1() {         while playing ==  true{             for a in 1...4{                 t1 = CFAbsoluteTimeGetCurrent()                 Snare(vol: 2)                 t2 = CFAbsoluteTimeGetCurrent()                 while t2 - t1 < (1) {                     t2 = CFAbsoluteTimeGetCurrent()                 }             }         }     } }//end of struct PlaygroundPage.current.setLiveView(DrumMachine()) PlaygroundPage.current.wantsFullScreenLiveView = true
7
0
777
Jun ’21