I am trying to make an audio player in SwiftUI, Audio player should have these functionality.
Play/Stop Audio
Play in loop
Change volume through slider
Change audio pitch through slider.
There are two problem currently I am facing
audio player is not using volume and pitch slider value
While I stop and play and change volume/pitch slider app crashes with following message.
2020-10-14 17:34:08.957709+0530 SwiftUIAudioPlayer[1369:24886] [avae]
AVAEInternal.h:109 [AVAudioFile.mm:484:-[AVAudioFile
readIntoBuffer:frameCount:error:]:
(ExtAudioFileRead(_imp->_extAudioFile, &ioFrames,
buffer.mutableAudioBufferList)): error -50
Here is the link to project. https://github.com/varun-naharia/SwiftUIAudioPlayer
ContentView.swift
import Foundation
import SwiftUI
struct ContentView: View {
#State var volume:Double = 0.00
#State var pitch:Double = 0.0
#State var musicFiles:[SoundModel] = [SoundModel(file: "metro35", name: "Metronome", fileExtension: "wav"), SoundModel(file: "johnson_tone_down_5min", name: "Johnson", fileExtension: "wav"), SoundModel(file: "sine_140_6s_fade_ogg", name: "Sine wave", fileExtension: "wav")]
#State var selectedMusicFile:SoundModel = SoundModel(file: "sine_140_6s_fade_ogg", name: "Sine wave", fileExtension: "wav")
#State var showSoundPicker = false
#State var selectedGraph = "skin_conductance"
#State var iconSize:CGFloat = 0.124
#State var iconSpace:CGFloat = 0.015
#State var heart = false
init() {
Player.setPitch(pitch: Float(self.pitch))
Player.setVolume(volume: Float(self.volume))
}
var body: some View {
GeometryReader { geometry in
ZStack{
VStack(alignment: .leading) {
Button(action: {
self.heart = !self.heart
self.selectedGraph = "heart"
if(self.heart)
{
Player.playMusic(musicfile: self.selectedMusicFile.file, fileExtension: self.selectedMusicFile.fileExtension)
}
else
{
Player.stopMusic()
self.selectedGraph = ""
}
})
{
Image(self.selectedGraph == "heart" ? "heart" : "heart_disabled")
.resizable()
.frame(width: geometry.size.height*self.iconSize, height: geometry.size.height*self.iconSize)
}
.frame(width: geometry.size.height*self.iconSize, height: geometry.size.height*self.iconSize)
.padding(.bottom, geometry.size.height*(self.iconSpace/2))
Button(action: {
self.showSoundPicker = !self.showSoundPicker
})
{
Image("tone")
.resizable()
.frame(width: geometry.size.height*self.iconSize, height: geometry.size.height*self.iconSize)
}
.frame(width: geometry.size.height*self.iconSize, height: geometry.size.height*self.iconSize)
.padding(.bottom, geometry.size.height*(self.iconSpace/2))
HStack{
SwiftUISlider(
thumbColor: .green,
thumbImage: "musicNote 2",
value: self.$volume
).padding(.horizontal)
Button(action: {
})
{
Image("centerGraph")
.resizable()
.frame(width: geometry.size.width*0.05, height: geometry.size.width*0.05)
}
.frame(width: geometry.size.width*0.03, height: geometry.size.width*0.03)
SwiftUISlider(
thumbColor: .green,
thumbImage: "timerSlider 2",
minValue: 0,
maxValue: 20,
value: self.$pitch
)
.padding(.horizontal)
.frame(width: (geometry.size.width/2)-geometry.size.width*0.05, height: geometry.size.width*0.05)
}
.background(Color(UIColor.lightGray))
.frame(width: geometry.size.width, height: geometry.size.height*0.10)
if(self.showSoundPicker)
{
ChooseSoundView(
musicFiles: self.musicFiles,
selectedMusicFile: self.$selectedMusicFile ,
showSoundPicker: self.$showSoundPicker,
isPlaying: self.selectedGraph != ""
)
.frame(width: geometry.size.width*0.6, height: geometry.size.height*0.7, alignment: .center)
.background(Color.white)
}
}
.frame(maxWidth: geometry.size.width,
maxHeight: geometry.size.height)
.background(Color(UIColor.lightGray))
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct ChooseSoundView: View {
#State var musicFiles:[SoundModel]
#Binding var selectedMusicFile:SoundModel
#Binding var showSoundPicker:Bool
#State var isPlaying:Bool
var body: some View {
GeometryReader { geometry in
VStack(alignment: .leading)
{
List(self.musicFiles, id: \.name)
{ item in
Image(self.selectedMusicFile.file == item.file ? "radio-button_on" : "radio-button_off")
.resizable()
.frame(width: 15, height: 15)
Button(action: {
print(item.name)
self.selectedMusicFile = item
self.showSoundPicker = false
if(self.isPlaying)
{
// Player.stopMusic()
// Player.playMusic(musicfile: self.selectedMusicFile.file, fileExtension: self.selectedMusicFile.fileExtension)
}
}){
Text(item.name)
.frame(width: geometry.size.width*90,
height: 50.0,
alignment: .leading)
}
.frame(width: geometry.size.width*90, height: 50.0)
}
HStack{
Button(action: {
self.showSoundPicker = false
}){
Text("Done")
.frame(width: geometry.size.width*0.45,
height: 50.0,
alignment: .center)
}
.frame(width: geometry.size.width*0.45, height: 50.0)
Button(action: {
self.showSoundPicker = false
}){
Text("Cancel")
.frame(width: geometry.size.width*0.45,
height: 50.0,
alignment: .center)
}
.frame(width: geometry.size.width*0.45, height: 50.0)
}
.background(Color.white)
}
}
}
}
Player.swift
import Foundation
import AVFoundation
class Player {
private static var breathAudioPlayer:AVAudioPlayer?
private static var audioPlayerEngine = AVAudioEngine()
private static let speedControl = AVAudioUnitVarispeed()
private static var pitchControl = AVAudioUnitTimePitch()
private static var audioPlayerNode = AVAudioPlayerNode()
private static var volume:Float = 1.0
private static func playSounds(soundfile: String) {
if let path = Bundle.main.path(forResource: soundfile, ofType: "m4a"){
do{
breathAudioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
breathAudioPlayer?.volume = self.volume
breathAudioPlayer?.prepareToPlay()
breathAudioPlayer?.play()
}catch {
print("Error")
}
}
}
static func playMusic(musicfile: String, fileExtension:String) {
if let path = Bundle.main.path(forResource: musicfile, ofType: fileExtension){
do{
// 1: load the file
let audioPlayFile = try AVAudioFile(forReading: URL(fileURLWithPath: path))
let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioPlayFile.fileFormat, frameCapacity: AVAudioFrameCount(audioPlayFile.length))
try? audioPlayFile.read(into: audioFileBuffer!)
// 2: create the audio player
audioPlayerNode = AVAudioPlayerNode()
audioPlayerEngine = AVAudioEngine()
// you can replace mp3 with anything else you like, just make sure you load it from our project
// making sure to clean up the audio hardware to avoid any damage and bugs
audioPlayerNode.stop()
audioPlayerEngine.stop()
audioPlayerEngine.reset()
audioPlayerEngine.attach(audioPlayerNode)
let pitchControl = AVAudioUnitTimePitch()
// assign the speed and pitch
audioPlayerEngine.attach(pitchControl)
audioPlayerEngine.connect(audioPlayerNode, to: pitchControl, format: nil)
audioPlayerEngine.connect(pitchControl, to: audioPlayerEngine.outputNode, format: nil)
audioPlayerNode.scheduleFile(audioPlayFile, at: nil, completionHandler: nil)
// try to start playing the audio
audioPlayerNode.scheduleBuffer(audioFileBuffer!, at: nil, options: .loops, completionHandler: nil)
do {
try audioPlayerEngine.start()
} catch {
print(error)
}
// play the audio
audioPlayerNode.play()
}catch {
print("Error")
}
}
}
static func breathIn() {
// Player.playSounds(soundfile: "breathin")
}
static func breathOut() {
// Player.playSounds(soundfile: "breathout")
}
static func play(musicFile:String, fileExtension:String)
{
Player.playMusic(musicfile: musicFile,fileExtension: fileExtension)
}
static func stopMusic() {
audioPlayerNode.pause()
audioPlayerNode.stop()
}
static func setPitch(pitch:Float) {
pitchControl.pitch = pitch
}
static func setVolume(volume:Float) {
audioPlayerNode.volume = volume
}
}
SwiftUISlider.swift
import Foundation
import SwiftUI
struct SwiftUISlider: UIViewRepresentable {
var onChangeNotification:String = ""
final class Coordinator: NSObject {
// The class property value is a binding: Itβs a reference to the SwiftUISlider
// value, which receives a reference to a #State variable value in ContentView.
var value: Binding<Double>
// Create the binding when you initialize the Coordinator
init(value: Binding<Double>) {
self.value = value
}
// Create a valueChanged(_:) action
#objc func valueChanged(_ sender: UISlider) {
self.value.wrappedValue = Double(sender.value)
}
}
var thumbColor: UIColor = .white
var minTrackColor: UIColor?
var maxTrackColor: UIColor?
var thumbImage:String?
var minValue:Float?
var maxValue:Float?
#Binding var value: Double
func makeUIView(context: Context) -> UISlider {
let slider = UISlider(frame: .zero)
slider.thumbTintColor = thumbColor
slider.minimumTrackTintColor = minTrackColor
slider.maximumTrackTintColor = maxTrackColor
slider.value = Float(value)
if(self.minValue != nil)
{
slider.minimumValue = self.minValue!
}
if(self.maxValue != nil)
{
slider.maximumValue = self.maxValue!
}
slider.setThumbImage(UIImage(named: self.thumbImage ?? ""), for: .normal)
slider.setThumbImage(UIImage(named: self.thumbImage ?? ""), for: .focused)
slider.setThumbImage(UIImage(named: self.thumbImage ?? ""), for: .highlighted)
slider.addTarget(
context.coordinator,
action: #selector(Coordinator.valueChanged(_:)),
for: .valueChanged
)
return slider
}
func onValueChange(_ sender: UISlider) {
}
func updateUIView(_ uiView: UISlider, context: Context) {
// Coordinating data between UIView and SwiftUI view
uiView.value = Float(self.value)
}
func makeCoordinator() -> SwiftUISlider.Coordinator {
Coordinator(value: $value)
}
}
SoundModel.swift
import Foundation
import Combine
class SoundModel:ObservableObject, Identifiable
{
#Published var file:String
#Published var name:String
#Published var fileExtension:String
init(file:String, name:String, fileExtension:String) {
self.file = file
self.name = name
self.fileExtension = fileExtension
}
}
Your first problem is that you're not tracking changes of volume/pitch values. To do so move them to a class:
class PlayerSetup: ObservableObject {
#Published var volume:Double = 0.00 {
didSet {
Player.setVolume(volume: Float(self.volume))
}
}
#Published var pitch:Double = 0.0{
didSet {
Player.setPitch(pitch: Float(self.pitch))
}
}
}
Declare in the view:
#ObservedObject var playerSetup = PlayerSetup()
And bind to your views:
SwiftUISlider(
thumbColor: .green,
thumbImage: "musicNote 2",
value: $playerSetup.volume
).padding(.horizontal)
SwiftUISlider(
thumbColor: .green,
thumbImage: "timerSlider 2",
minValue: 0,
maxValue: 20,
value: $playerSetup.pitch
)
it crashes when finish playing the file because try? audioPlayFile.read(into: audioFileBuffer!) fails and your buffer scheduled after the file is empty. and it plays the file for the first time because of scheduleFile. If you wanna loop single file, try calling this function:
static func scheduleNext(audioPlayFile: AVAudioFile) {
audioPlayerNode.scheduleFile(audioPlayFile, at: nil) {
DispatchQueue.main.async {
scheduleNext(audioPlayFile: audioPlayFile)
}
}
}
pitchControl doesn't work because you're using local value when starting playing, just remove the local value declaration.
about to the volume. as you can see in the documentation, This property is implemented only by the AVAudioEnvironmentNode and AVAudioMixerNode class mixers. So you can't use it for a player node, you need to create a mixer node, add it to the chain of nodes, and change it's volume.
Also to clean up nodes code, I advice you to use the following code:
let nodes = [
audioPlayerNode,
pitchControl,
mixerNode,
]
nodes.forEach { node in
audioPlayerEngine.attach(node)
}
zip(nodes, (nodes.dropFirst() + [audioPlayerEngine.outputNode]))
.forEach { firstNode, secondNode in
audioPlayerEngine.connect(firstNode, to: secondNode, format: nil)
}
it connects all nodes one by one.
https://github.com/PhilipDukhov/SwiftUIAudioPlayer/tree/fixes
Main problem of your code that there is mixed view and view model between them. Lot of things that MUST BE in viewModel at the moment located in the View.
View:
struct ContentView: View {
#State var volume:Double = 0.00
#State var pitch:Double = 0.0
#State var musicFiles:[SoundModel] = [SoundModel(file: "metro35", name: "Metronome", fileExtension: "wav"), SoundModel(file: "johnson_tone_down_5min", name: "Johnson", fileExtension: "wav"), SoundModel(file: "sine_140_6s_fade_ogg", name: "Sine wave", fileExtension: "wav")]
#State var selectedMusicFile:SoundModel = SoundModel(file: "sine_140_6s_fade_ogg", name: "Sine wave", fileExtension: "wav")
#State var showSoundPicker = false
#State var selectedGraph = "skin_conductance"
#State var iconSize:CGFloat = 0.124
#State var iconSpace:CGFloat = 0.015
#State var heart = false
init() {
Player.setPitch(pitch: Float(self.pitch))
Player.setVolume(volume: Float(self.volume))
}
view must be:
struct PlayerView: View {
#ObservedObject var viewModel: PlayerViewModel
#State var iconSize:CGFloat = 0.124
#State var iconSpace:CGFloat = 0.015
init() {
// that was wrote here must be moved to init of playerViewModel
}
viewModel your:
import Foundation
import AVFoundation
class Player {
private static var breathAudioPlayer:AVAudioPlayer?
private static var audioPlayerEngine = AVAudioEngine()
private static let speedControl = AVAudioUnitVarispeed()
private static var pitchControl = AVAudioUnitTimePitch()
private static var audioPlayerNode = AVAudioPlayerNode()
private static var volume:Float = 1.0
private static func playSounds(soundfile: String) {
....
ViewModel must be:
import Foundation
import AVFoundation
class PlayerViewModel: ObservableObject {
#Published var volume:Double = 0.00
#Published var pitch:Double = 0.0
#Published var musicFiles:[SoundModel] = [SoundModel(file: "metro35", name: "Metronome", fileExtension: "wav"), SoundModel(file: "johnson_tone_down_5min", name: "Johnson", fileExtension: "wav"), SoundModel(file: "sine_140_6s_fade_ogg", name: "Sine wave", fileExtension: "wav")]
#Published var selectedMusicFile:SoundModel = SoundModel(file: "sine_140_6s_fade_ogg", name: "Sine wave", fileExtension: "wav")
#Published var showSoundPicker = false
#Published var selectedGraph = "skin_conductance"
//#Published var heart = false // SoundViewModel's property
private static var breathAudioPlayer:AVAudioPlayer?
private static var audioPlayerEngine = AVAudioEngine()
private static let speedControl = AVAudioUnitVarispeed()
private static var pitchControl = AVAudioUnitTimePitch()
private static var audioPlayerNode = AVAudioPlayerNode()
private static var volume:Float = 1.0
private static func playSounds(soundfile: String) {
If you will move all of this to proper place, almost sure your code will work better and possibly your problem will be fixed.
at the moment you need to track any changes manually. Your structure of code is the reason of this. You do not need to track this manually because of this is additional useless code.
You need to fix the code structure instead of use "didSet"
Related
I am trying to understand why when I add a photo in my view it is duplicating the photo?
I want the user to add a photo to each card separately. I have tried to directly add selectedImage to my struct item image within the [Expense] array but xcode is screaming at me.
I know it has to with the #State selectedImage with its #Binding but Im not 100% sure how to enable this to work in my current scenario?
Here is the struct it is conforming to:
struct Card: Identifiable {
var id = UUID()
var title: String
var expenses: [Expense]
mutating func addExpenses() {
expenses.append(Expense(expensetype: "", amount: 0.0))
}
}
struct Expense: Identifiable {
var id = UUID()
var expensetype: String
var amount: Double = 0.0
var image: UIImage?
}
I am creating an array of cards on a button press in my contentview {
struct ContentView: View {
#State private var cards = [Card]()
#State private var sourceType: UIImagePickerController.SourceType = .photoLibrary
#State private var selectedImage: UIImage?
#State private var isImagePickerDisplay = false
#State private var showingOptions = false
var title = ""
var expensetype = ""
var amount: Double = 0.0
var image: UIImage?
var body: some View {
NavigationStack {
Form {
List {
Button("Add card") {
addCard()
}
ForEach($cards) { a in
Section {
TextField("Title", text: a.title)
Button("Add expense") {
a.wrappedValue.addExpenses()
}
ForEach(a.expenses) { b in
if a.expenses.isEmpty == false {
TextField("my expense", text: b.expensetype)
TextField("amount", value: b.amount, format: .number)
Button("Add image") {
withAnimation {
showingOptions = true
}
}
.confirmationDialog("", isPresented: $showingOptions, titleVisibility: .hidden) {
Button("Take photo") {
self.sourceType = .camera
self.isImagePickerDisplay.toggle()
}
Button("Choose photo") {
self.sourceType = .photoLibrary
self.isImagePickerDisplay.toggle()
}
}
if selectedImage != nil {
Image(uiImage: self.selectedImage!)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 150, height: 150)
} else {
Image(systemName: "snow")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 150, height: 150)
}
}
}
}
}
}
}
.sheet(isPresented: self.$isImagePickerDisplay) {
ImagePickerView(selectedImage: self.$selectedImage, sourceType: self.sourceType)
}
}
}
func addCard() {
cards.append(Card(title: title, expenses: []))
}
}
Here is my ImagePickerView
struct ImagePickerView: UIViewControllerRepresentable {
#Binding var selectedImage: UIImage?
#Environment(\.presentationMode) var isPresented
var sourceType: UIImagePickerController.SourceType
func makeUIViewController(context: Context) -> some UIViewController {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = self.sourceType
imagePicker.delegate = context.coordinator
return imagePicker
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
func makeCoordinator() -> Coordinator {
return Coordinator(picker: self)
}
}
I want to create a Timer that is showing in a Slider.
I got this Code:
struct CustomSlider : View {
#State var value: Double = 0.0
init() {
let thumbImage = UIImage(systemName: "circle.fill")
UISlider.appearance().setThumbImage(thumbImage, for: .normal)
}
var body: some View {
Slider(value: $value)
}
}
struct MusicPlayerView: View {
#State var value: Double = 0.0
let timer = Timer
.publish(every: 0.5, on: .main, in: .common)
.autoconnect()
var body: some View {
CustomSlider()
.onReceive(timer) { _ in
guard let player = audioManager.player else {return}
value = player.currentTime
}
}
}
How do I pass the value from MusicPlayerView to CustomSlider? I tried using #Bindingin the CustomSliderbut then it gives me the error:
Return from initializer without initializing all stored properties
in CustomSlider
When using #Binding you need to update your init:
struct CustomSlider : View {
#Binding var value: Double
init(value: Binding<Double>) {
self._value = value
let thumbImage = UIImage(systemName: "circle.fill")
UISlider.appearance().setThumbImage(thumbImage, for: .normal)
}
var body: some View {
Slider(value: $value)
}
}
And in MusicPlayerView:
struct MusicPlayerView: View {
#State var value: Double = 0.0
let timer = Timer
.publish(every: 0.5, on: .main, in: .common)
.autoconnect()
var body: some View {
CustomSlider(value: $value)
.onReceive(timer) { _ in
guard let player = audioManager.player else {return}
value = player.currentTime
}
}
}
I'm losing my mind over this, please help
I'm following the standford's iOS tutorial, I'm trying to finish an assignment of creating a card games, I have 3 models, Game, Card, Theme and Themes:
Game and Card are in charge of the main game logic
import Foundation
struct Game {
var cards: [Card]
var score = 0
var isGameOver = false
var theme: Theme
var choosenCardIndex: Int?
init(theme: Theme) {
cards = []
self.theme = theme
startTheme()
}
mutating func startTheme() {
cards = []
var contentItems: [String] = []
while contentItems.count != theme.numberOfPairs {
let randomElement = theme.emojis.randomElement()!
if !contentItems.contains(randomElement) {
contentItems.append(randomElement)
}
}
let secondContentItems: [String] = contentItems.shuffled()
for index in 0..<theme.numberOfPairs {
cards.append(Card(id: index*2, content: contentItems[index]))
cards.append(Card(id: index*2+1, content: secondContentItems[index]))
}
}
mutating func chooseCard(_ card: Card) {
print(card)
if let foundIndex = cards.firstIndex(where: {$0.id == card.id}),
!cards[foundIndex].isFaceUp,
!cards[foundIndex].isMatchedUp
{
if let potentialMatchIndex = choosenCardIndex {
if cards[foundIndex].content == cards[potentialMatchIndex].content {
cards[foundIndex].isMatchedUp = true
cards[potentialMatchIndex].isMatchedUp = true
}
choosenCardIndex = nil
} else {
for index in cards.indices {
cards[index].isFaceUp = false
}
}
cards[foundIndex].isFaceUp.toggle()
}
print(card)
}
mutating func endGame() {
isGameOver = true
}
mutating func penalizePoints() {
score -= 1
}
mutating func awardPoints () {
score += 2
}
struct Card: Identifiable, Equatable {
static func == (lhs: Game.Card, rhs: Game.Card) -> Bool {
return lhs.content == rhs.content
}
var id: Int
var isFaceUp: Bool = false
var content: String
var isMatchedUp: Bool = false
var isPreviouslySeen = false
}
}
Theme is for modeling different kind of content, Themes is for keeping track which one is currently in use and for fetching a new one
import Foundation
import SwiftUI
struct Theme: Equatable {
static func == (lhs: Theme, rhs: Theme) -> Bool {
return lhs.name == rhs.name
}
internal init(name: String, emojis: [String], numberOfPairs: Int, cardsColor: Color) {
self.name = name
self.emojis = Array(Set(emojis))
if(numberOfPairs > emojis.count || numberOfPairs < 1) {
self.numberOfPairs = emojis.count
} else {
self.numberOfPairs = numberOfPairs
}
self.cardsColor = cardsColor
}
var name: String
var emojis: [String]
var numberOfPairs: Int
var cardsColor: Color
}
import Foundation
struct Themes {
private let themes: [Theme]
public var currentTheme: Theme?
init(_ themes: [Theme]) {
self.themes = themes
self.currentTheme = getNewTheme()
}
private func getNewTheme() -> Theme {
let themesIndexes: [Int] = Array(0..<themes.count)
var visitedIndexes: [Int] = []
while(visitedIndexes.count < themesIndexes.count) {
let randomIndex = Int.random(in: 0..<themes.count)
let newTheme = themes[randomIndex]
if newTheme == currentTheme {
visitedIndexes.append(randomIndex)
} else {
return newTheme
}
}
return themes.randomElement()!
}
mutating func changeCurrentTheme() -> Theme {
self.currentTheme = getNewTheme()
return self.currentTheme!
}
}
This is my VM:
class GameViewModel: ObservableObject {
static let numbersTheme = Theme(name: "WeirdNumbers", emojis: ["1", "2", "4", "9", "20", "30"], numberOfPairs: 6, cardsColor: .pink)
static let emojisTheme = Theme(name: "Faces", emojis: ["π₯°", "π", "π", "π₯³", "π€", "π", "π", "π€©"], numberOfPairs: 8, cardsColor: .blue)
static let carsTheme = Theme(name: "Cars", emojis: ["π", "ποΈ", "π", "π", "π", "π", "π", "π"], numberOfPairs: 20, cardsColor: .yellow)
static let activitiesTheme = Theme(name: "Activities", emojis: ["π€Ί", "ποΈ", "πββοΈ", "π£", "πββοΈ", "ποΈ", "π΄ββοΈ"], numberOfPairs: -10, cardsColor: .green)
static let fruitsTheme = Theme(name: "Fruits", emojis: ["π", "π", "π", "π", "π", "π", "π", "π₯"], numberOfPairs: 5, cardsColor: .purple)
static var themes = Themes([numbersTheme, emojisTheme, carsTheme, fruitsTheme])
static func createMemoryGame() -> Game {
Game(theme: themes.currentTheme!)
}
#Published private var gameController: Game = Game(theme: themes.currentTheme!)
func createNewGame() {
gameController.theme = GameViewModel.themes.changeCurrentTheme()
gameController.startTheme()
}
func choose(_ card: Game.Card) {
objectWillChange.send()
gameController.chooseCard(card)
}
var cards: [Game.Card] {
return gameController.cards
}
var title: String {
return gameController.theme.name
}
var color: Color {
return gameController.theme.cardsColor
}
}
And this is my view:
struct ContentView: View {
var columns: [GridItem] = [GridItem(.adaptive(minimum: 90, maximum: 400))]
#ObservedObject var ViewModel: GameViewModel
var body: some View {
VStack {
HStack {
Spacer()
Button(action: {
ViewModel.createNewGame()
}, label: {
VStack {
Image(systemName: "plus")
Text("New game")
.font(/*#START_MENU_TOKEN#*/.caption/*#END_MENU_TOKEN#*/)
}
})
.font(/*#START_MENU_TOKEN#*/.title/*#END_MENU_TOKEN#*/)
.padding(.trailing)
}
Section {
VStack {
Text(ViewModel.title)
.foregroundColor(/*#START_MENU_TOKEN#*/.blue/*#END_MENU_TOKEN#*/)
.font(/*#START_MENU_TOKEN#*/.title/*#END_MENU_TOKEN#*/)
}
}
ScrollView {
LazyVGrid(columns: columns ) {
ForEach(ViewModel.cards, id: \.id) { card in
Card(card: card, color: ViewModel.color)
.aspectRatio(2/3, contentMode: .fit)
.onTapGesture {
ViewModel.choose(card)
}
}
}
.font(.largeTitle)
}
.padding()
Text("Score")
.frame(maxWidth: .infinity, minHeight: 30)
.background(Color.blue)
.foregroundColor(/*#START_MENU_TOKEN#*/.white/*#END_MENU_TOKEN#*/)
Spacer()
HStack {
Spacer()
Text("0")
.font(.title2)
.bold()
Spacer()
}
}
}
}
struct Card: View {
let card: Game.Card
let color: Color
var body: some View {
ZStack {
let shape = RoundedRectangle(cornerRadius: 10)
if card.isFaceUp {
Text(card.content)
shape
.strokeBorder()
.accentColor(color)
.foregroundColor(color)
}
else {
shape
.fill(color)
}
}
}
}
Basically the problem lies with the
.onTapGesture {
ViewModel.choose(card)
}
Of the View, when someone taps a card, the isFaceUp property of the Card is changed to true, but this doesn't get reflected in the UI.
If I generate a new view by changing the theme and adding new cards, this works.
Button(action: {
ViewModel.createNewGame()
}, label: {
VStack {
Image(systemName: "plus")
Text("New game")
.font(/*#START_MENU_TOKEN#*/.caption/*#END_MENU_TOKEN#*/)
}
})
But when I'm trying to flip a card it doesn't work, the value changes in the Game model but it's not updated on the view
After the tap the ViewModel calls the choose method
func choose(_ card: Game.Card) {
gameController.chooseCard(card)
}
And this changed the value of the Model in the Game.swift file by calling the chooseCard method
mutating func chooseCard(_ card: Card) {
print(card)
if let foundIndex = cards.firstIndex(where: {$0.id == card.id}),
!cards[foundIndex].isFaceUp,
!cards[foundIndex].isMatchedUp
{
if let potentialMatchIndex = choosenCardIndex {
if cards[foundIndex].content == cards[potentialMatchIndex].content {
cards[foundIndex].isMatchedUp = true
cards[potentialMatchIndex].isMatchedUp = true
}
choosenCardIndex = nil
} else {
for index in cards.indices {
cards[index].isFaceUp = false
}
}
cards[foundIndex].isFaceUp.toggle()
}
print(card)
}
The values changes but the view does not, the gameController variable of the GameViewModel has the #Published state, which points to an instance of the Game model struct
#Published private var gameController: Game = Game(theme: themes.currentTheme!)
And the view it's accesing this GameViewModel with the #ObservedObject property
#ObservedObject var ViewModel: GameViewModel
I thought I was doing everything right, but I guess not lol, what the heck am I doing wrong? Why can't update my view if I'm using published and observable object on my ViewModel? lol
The main reason the card view doesn't see changes is because in your card view you did put an equatable conformance protocol where you specify an equality check == function that just checks for content and not other variable changes
static func ==(lhs: Game.Card, rhs: Game.Card) -> Bool {
lhs.content == rhs.content
// && lhs.isFaceUp && rhs.isFaceUp //<- you can still add this
}
if you remove the equatable protocol and leave swift to check for equality it should be the minimal change from your base solution.
I would still use the solution where you change the state of the class card so the view can react to changes as an ObservableObject, and the #Published for changes that the view need to track, like this:
class Card: Identifiable, Equatable, ObservableObject {
var id: Int
#Published var isFaceUp: Bool = false
var content: String
#Published var isMatchedUp: Bool = false
var isPreviouslySeen = false
internal init(id: Int, content: String) {
self.id = id
self.content = content
}
static func ==(lhs: Game.Card, rhs: Game.Card) -> Bool {
lhs.content == rhs.content
}
}
and in the Card view the card variable will become
struct Card: View {
#ObservedObject var card: Game.Card
...
}
btw you don't need to notify the view of changes with
objectWillChange.send() if you are already using the #Published notation. every set to the variable will trigger an update.
you could try this instead of declaring Card a class:
Card(card: card, color: ViewModel.color, isFaceUp: card.isFaceUp)
and add this to the Card view:
let isFaceUp: Bool
My understanding is that the Card view does not see any changes to the card (not sure why, maybe because it is in an if),
but if you give it something that has really changed then it is re-rendered. And as mentioned before no need for objectWillChange.send()
EDIT1:
you could also do this in "ContentView":
Card(viewModel: ViewModel, card: card)
and then
struct Card: View {
#ObservedObject var viewModel: GameViewModel
let card: Game.Card
var body: some View {
ZStack {
let shape = RoundedRectangle(cornerRadius: 10)
if card.isFaceUp {
Text(card.content)
shape
.strokeBorder()
.accentColor(viewModel.color)
.foregroundColor(viewModel.color)
}
else {
shape.fill(viewModel.color)
}
}
}
}
I'm working on my project with the feature of select multiple blocks of thumbnails. Only selected thumbnail(s)/image will be highlighted.
For the ChildView, The binding activeBlock should be turned true/false if a use taps on the image.
However, when I select a thumbnail, all thumbnails will be highlighted.I have come up with some ideas like
#State var selectedBlocks:[Bool]
// which should contain wether or not a certain block is selected.
But I don't know how to implement it.
Here are my codes:
ChildView
#Binding var activeBlock:Bool
var thumbnail: String
var body: some View {
VStack {
ZStack {
Image(thumbnail)
.resizable()
.frame(width: 80, height: 80)
.background(Color.black)
.cornerRadius(10)
if activeBlock {
RoundedRectangle(cornerRadius: 10)
.stroke(style: StrokeStyle(lineWidth: 2))
.frame(width: 80, height: 80)
.foregroundColor(Color("orange"))
}
}
}
BlockBView
struct VideoData: Identifiable{
var id = UUID()
var thumbnails: String
}
struct BlockView: View {
var videos:[VideoData] = [
VideoData(thumbnails: "test"), VideoData(thumbnails: "test2"), VideoData(thumbnails: "test1")
]
#State var activeBlock = false
var body: some View {
ScrollView(.horizontal){
HStack {
ForEach(0..<videos.count) { _ in
Button(action: {
self.activeBlock.toggle()
}, label: {
ChildView(activeBlock: $activeBlock, thumbnail: "test")
})
}
}
}
}
Thank you for your help!
Here is a demo of possible approach - we initialize array of Bool by videos count and pass activated flag by index into child view.
Tested with Xcode 12.1 / iOS 14.1 (with some replicated code)
struct BlockView: View {
var videos:[VideoData] = [
VideoData(thumbnails: "flag-1"), VideoData(thumbnails: "flag-2"), VideoData(thumbnails: "flag-3")
]
#State private var activeBlocks: [Bool] // << declare
init() {
// initialize state with needed count of bools
self._activeBlocks = State(initialValue: Array(repeating: false, count: videos.count))
}
var body: some View {
ScrollView(.horizontal){
HStack {
ForEach(videos.indices, id: \.self) { i in
Button(action: {
self.activeBlocks[i].toggle() // << here !!
}, label: {
ChildView(activeBlock: activeBlocks[i], // << here !!
thumbnail: videos[i].thumbnails)
})
}
}
}
}
}
struct ChildView: View {
var activeBlock:Bool // << value, no binding needed
var thumbnail: String
var body: some View {
VStack {
ZStack {
Image(thumbnail)
.resizable()
.frame(width: 80, height: 80)
.background(Color.black)
.cornerRadius(10)
if activeBlock {
RoundedRectangle(cornerRadius: 10)
.stroke(style: StrokeStyle(lineWidth: 2))
.frame(width: 80, height: 80)
.foregroundColor(Color.orange)
}
}
}
}
}
Final result
Build your element and it's model first. I'm using MVVM,
class RowModel : ObservableObject, Identifiable {
#Published var isSelected = false
#Published var thumnailIcon: String
#Published var name: String
var id : String
var cancellables = Set<AnyCancellable>()
init(id: String, name: String, icon: String) {
self.id = id
self.name = name
self.thumnailIcon = icon
}
}
//Equivalent to your BlockView
struct Row : View {
#ObservedObject var model: RowModel
var body: some View {
GroupBox(label:
Label(model.name, systemImage: model.thumnailIcon)
.foregroundColor(model.isSelected ? Color.orange : .gray)
) {
HStack {
Capsule()
.fill(model.isSelected ? Color.orange : .gray)
.onTapGesture {
model.isSelected = !model.isSelected
}
//Two way binding
Toggle("", isOn: $model.isSelected)
}
}.animation(.spring())
}
}
Prepare data and handle action in your parent view
struct ContentView: View {
private let layout = [GridItem(.flexible()),GridItem(.flexible())]
#ObservedObject var model = ContentModel()
var body: some View {
VStack {
ScrollView {
LazyVGrid(columns: layout) {
ForEach(model.rowModels) { model in
Row(model: model)
}
}
}
if model.selected.count > 0 {
HStack {
Text(model.selected.joined(separator: ", "))
Spacer()
Button(action: {
model.clearSelection()
}, label: {
Text("Clear")
})
}
}
}
.padding()
.onAppear(perform: prepare)
}
func prepare() {
model.prepare()
}
}
class ContentModel: ObservableObject {
#Published var rowModels = [RowModel]()
//I'm handling by ID for futher use
//But you can convert to your Array of Boolean
#Published var selected = Set<String>()
func prepare() {
for i in 0..<20 {
let row = RowModel(id: "\(i)", name: "Block \(i)", icon: "heart.fill")
row.$isSelected
.removeDuplicates()
.receive(on: RunLoop.main)
.sink(receiveValue: { [weak self] selected in
guard let `self` = self else { return }
print(selected)
if selected {
self.selected.insert(row.name)
}else{
self.selected.remove(row.name)
}
}).store(in: &row.cancellables)
rowModels.append(row)
}
}
func clearSelection() {
for r in rowModels {
r.isSelected = false
}
}
}
Don't forget to import Combine framework.
I'm creating a custom stepper control in SwiftUI, and I'm trying to replicate the accelerating value change behavior of the built-in control. In a SwiftUI Stepper, long pressing on "+" or "-" will keep increasing/decreasing the value with the rate of change getting faster the longer you hold the button.
I can create the visual effect of holding down the button with the following:
struct PressBox: View {
#GestureState var pressed = false
#State var value = 0
var body: some View {
ZStack {
Rectangle()
.fill(pressed ? Color.blue : Color.green)
.frame(width: 70, height: 50)
.gesture(LongPressGesture(minimumDuration: .infinity)
.updating($pressed) { value, state, transaction in
state = value
}
.onChanged { _ in
self.value += 1
}
)
Text("\(value)")
.foregroundColor(.white)
}
}
}
This only increments the value once. Adding a timer publisher to the onChanged modifier for the gesture like this:
let timer = Timer.publish(every: 0.5, on: .main, in: .common)
#State var cancellable: AnyCancellable? = nil
...
.onChanged { _ in
self.cancellable = self.timer.connect() as? AnyCancellable
}
will replicate the changing values, but since the gesture never completes successfully (onEnded will never be called), there's no way to stop the timer. Gestures don't have an onCancelled modifier.
I also tried doing this with a TapGesture which would work for detecting the end of the gesture, but I don't see a way to detect the start of the gesture. This code:
.gesture(TapGesture()
.updating($pressed) { value, state, transaction in
state = value
}
)
generates an error on $pressed:
Cannot convert value of type 'GestureState' to expected argument type 'GestureState<_>'
Is there a way to replicate the behavior without falling back to UIKit?
You'd need an onTouchDown event on the view to start a timer and an onTouchUp event to stop it. SwiftUI doesn't provide a touch down event at the moment, so I think the best way to get what you want is to use the DragGesture this way:
import SwiftUI
class ViewModel: ObservableObject {
private static let updateSpeedThresholds = (maxUpdateSpeed: TimeInterval(0.05), minUpdateSpeed: TimeInterval(0.3))
private static let maxSpeedReachedInNumberOfSeconds = TimeInterval(2.5)
#Published var val: Int = 0
#Published var started = false
private var timer: Timer?
private var currentUpdateSpeed = ViewModel.updateSpeedThresholds.minUpdateSpeed
private var lastValueChangingDate: Date?
private var startDate: Date?
func start() {
if !started {
started = true
val = 0
startDate = Date()
startTimer()
}
}
func stop() {
timer?.invalidate()
currentUpdateSpeed = Self.updateSpeedThresholds.minUpdateSpeed
lastValueChangingDate = nil
started = false
}
private func startTimer() {
timer = Timer.scheduledTimer(withTimeInterval: Self.updateSpeedThresholds.maxUpdateSpeed, repeats: false) {[unowned self] _ in
self.updateVal()
self.updateSpeed()
self.startTimer()
}
}
private func updateVal() {
if self.lastValueChangingDate == nil || Date().timeIntervalSince(self.lastValueChangingDate!) >= self.currentUpdateSpeed {
self.lastValueChangingDate = Date()
self.val += 1
}
}
private func updateSpeed() {
if self.currentUpdateSpeed < Self.updateSpeedThresholds.maxUpdateSpeed {
return
}
let timePassed = Date().timeIntervalSince(self.startDate!)
self.currentUpdateSpeed = timePassed * (Self.updateSpeedThresholds.maxUpdateSpeed - Self.updateSpeedThresholds.minUpdateSpeed)/Self.maxSpeedReachedInNumberOfSeconds + Self.updateSpeedThresholds.minUpdateSpeed
}
}
struct ContentView: View {
#ObservedObject var viewModel: ViewModel
var body: some View {
ZStack {
Rectangle()
.fill(viewModel.started ? Color.blue : Color.green)
.frame(width: 70, height: 50)
.gesture(DragGesture(minimumDistance: 0)
.onChanged { _ in
self.viewModel.start()
}
.onEnded { _ in
self.viewModel.stop()
}
)
Text("\(viewModel.val)")
.foregroundColor(.white)
}
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(viewModel: ViewModel())
}
}
#endif
Let me know if I got what you wanted or whether I can improve my answer somehow.
For anyone attempting something similar, here's a slightly different take on superpuccio's approach. The api for users of the type is a bit more straightforward, and it minimizes the number of timer fires as the speed ramps up.
struct TimerBox: View {
#Binding var value: Int
#State private var isRunning = false
#State private var startDate: Date? = nil
#State private var timer: Timer? = nil
private static let thresholds = (slow: TimeInterval(0.3), fast: TimeInterval(0.05))
private static let timeToMax = TimeInterval(2.5)
var body: some View {
ZStack {
Rectangle()
.fill(isRunning ? Color.blue : Color.green)
.frame(width: 70, height: 50)
.gesture(DragGesture(minimumDistance: 0)
.onChanged { _ in
self.startRunning()
}
.onEnded { _ in
self.stopRunning()
}
)
Text("\(value)")
.foregroundColor(.white)
}
}
private func startRunning() {
guard isRunning == false else { return }
isRunning = true
startDate = Date()
timer = Timer.scheduledTimer(withTimeInterval: Self.thresholds.slow, repeats: true, block: timerFired)
}
private func timerFired(timer: Timer) {
guard let startDate = self.startDate else { return }
self.value += 1
let timePassed = Date().timeIntervalSince(startDate)
let newSpeed = Self.thresholds.slow - timePassed * (Self.thresholds.slow - Self.thresholds.fast)/Self.timeToMax
let nextFire = Date().advanced(by: max(newSpeed, Self.thresholds.fast))
self.timer?.fireDate = nextFire
}
private func stopRunning() {
timer?.invalidate()
isRunning = false
}
}