在 SwiftUI 中播放音频时无法更新行颜色 [英] Cannot update row color while playing audio, in SwiftUI
问题描述
在使用 SwiftUI
开发 iOS
应用程序时,我面临以下情况.我已经设置了一个自定义环境键.这是 SceneDelegate.swift 文件中的相关代码.
While working on an iOS
app using SwiftUI
, I am facing the following situation. I have set a custom environment key. Here is the relevant code inside the SceneDelegate.swift file.
private struct RowColorMaster: EnvironmentKey {
static var defaultValue: Binding<[[String:Color]]> = .constant([[:]])
}
extension EnvironmentValues {
var rowColorMasterDico: Binding<[[String:Color]]> {
get {self[RowColorMaster.self]}
set {self[RowColorMaster.self] = newValue}
}
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
var rowColorMasterDico:[[String:Color]] = []
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
.....
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
.....
rowColorMasterDico = .....
print("rowColorMasterDico -> \(rowColorMasterDico.count)")
let contentView = ContentView(sectionsList: sectionsList,
itemMasterList: sentceMasterArray)
.environment(\.managedObjectContext, context)
.environment(\.rowColorMasterDico,
Binding(get: {self.rowColorMasterDico},
set: {
newValue in
self.rowColorMasterDico = newValue
}))
.....
}
.....
}
再往下,在视图层次结构中,在一些视图文件中:
Then further down, in the view hierarchy, in some view file:
struct SectionView: View {
var moc:NSManagedObjectContext
.....
@Environment(\.rowColorMasterDico) var rowColorMasterDico
.....
func handleEvent(_ file: String, flag: Bool) {
// This function is called when an audio file
// starts or ends playing.
if rowColorMasterDico.wrappedValue.count != 10 {
// This should not happen! But for some reason it sometimes does.
print("rowColorMasterDico is messed up !")
return
}
// Check that rowColorMasterDico.wrappedValue[page.index][file] changes value.
print("CLR(Before)=\(rowColorMasterDico.wrappedValue[page.index][file]!)")
rowColorMasterDico.wrappedValue[page.index][file] = flag ? Color.red : Color.white
print("CLR(After)=\(rowColorMasterDico.wrappedValue[page.index][file]!)")
}
.....
var body: some View {
VStack {
ListItemView(itemList: itmMstrList[page.index], moc: moc,
rowColorDico: rowColorMasterDico.wrappedValue[page.index])
Button(action: {
if !soundManager.isPlaying {self.playAllAudio(itmMstrList[page.index])}
else {soundManager.stopAudio()}
})
{
Image(uiImage: UIImage(named: "speaker.png")!)
.padding(.bottom, 17)
}
}
}
.....
struct ListItemView: View {
let crnrRad:CGFloat = 13.0, bgGreyLvl = 0.37, fgGreyLvl = 0.93
var itemList:[ItemList], moc:NSManagedObjectContext
@State var rowColorDico:[String:Color]
.....
func didDismiss() {
// Handle the dismissing action.
print(#function)
}
var body: some View {
List {
ForEach(self.itemList) {
item in
HStack {
Spacer()
Button(action: {
self.handleRowItem(item)
})
{
RowItemView(expression: item.expression!,
fgTxtColor: rowColorDico[item.soundFile!] ?? Color.orange)
}
Spacer()
}
}
}.sheet(item: $itemPage, onDismiss: didDismiss) {
sntnc in
.....
}
}
}
struct RowItemView: View {
var expression:String
@State var fgTxtColor:Color
let crnrRad:CGFloat = 13.0, bgGreyLvl = 0.37
var body: some View {
Text(expression)
.foregroundColor(fgTxtColor)
.font(.headline)
.padding(.horizontal, 11).padding(.vertical, 15)
.background(Color(.sRGB, red: bgGreyLvl, green: bgGreyLvl,
blue:bgGreyLvl, opacity: 1.0))
.cornerRadius(crnrRad)
.overlay(RoundedRectangle(cornerRadius: crnrRad)
.stroke(Color.yellow, lineWidth: 3))
}
}
问题来了.当我在 ListItemView 中播放音频文件列表时,匹配的行项目应该更改播放文件的颜色.该工作在 handleEvent 函数内完成,我可以检查这是否按预期发生.但是列表中显示的颜色不会改变.这是为什么?
Here is the problem. When I play the list of audio files in ListItemView the matching row item should change color for the playing file. The work for that is done inside the handleEvent function and I can check that this is happening as expected. But the color displayed in the list does not change. Why is that?
我设置环境变量 rowColorMasterDico 的方式有误吗?还是我错过了其他东西?我已经尝试了上面代码的许多不同变体,但我无法让它工作.我希望有人能看到并指出某个地方的错误.
Did I make a mistake in the way I set the environment variable rowColorMasterDico? Or am I missing something else? I have tried a number different variations of the code above, but I couldn't make it work. I hope someone will see and point out a mistake somewhere.
推荐答案
以防万一这对其他人有用,我想报告我通过参考 this 同时再看看我的自己的代码.
Just in case this may be useful to others, I want to report that I got my problem solved by referring to this while take a second look at my own code.
这篇关于在 SwiftUI 中播放音频时无法更新行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!