質問編集履歴
1
codeが読みやすいように修正しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,61 +1,63 @@
|
|
1
|
-
画面
|
1
|
+
2番目の画面から、画面遷移と同時に音源(assets保存)を停止したいのですが、初期画面に戻っても音源は再生をされたままです。画面遷移は成功しています。初心者ですのでよろしくお願いします。
|
2
|
+
|
2
|
-
|
3
|
+
```ここに言語を入力
|
3
4
|
import SwiftUI
|
4
5
|
import AVFoundation
|
5
6
|
struct ContentView: View {
|
6
|
-
|
7
|
+
var body: some View {
|
7
|
-
|
8
|
+
ZStack {
|
8
|
-
|
9
|
+
NavigationView {
|
9
|
-
|
10
|
+
ZStack{
|
10
|
-
|
11
|
+
Image("imgkeikan")
|
11
|
-
|
12
|
+
.resizable()
|
12
|
-
|
13
|
+
.edgesIgnoringSafeArea(.all)
|
13
|
-
|
14
|
+
.aspectRatio(contentMode:.fill)
|
14
|
-
|
15
|
+
VStack(spacing:100) {
|
15
|
-
|
16
|
+
Text("いろいろなまつり")
|
16
|
-
|
17
|
+
.font(.system(size: 80))
|
17
|
-
|
18
|
+
.foregroundColor(Color.white)
|
18
|
-
|
19
|
+
.padding()
|
19
|
-
|
20
|
+
MaturiView()
|
20
|
-
|
21
|
+
.padding()
|
21
|
-
}
|
22
|
-
}
|
23
|
-
.navigationBarTitle(Text("はじめに"), displayMode: .inline)
|
24
|
-
}
|
25
|
-
.navigationViewStyle(StackNavigationViewStyle())
|
26
|
-
}
|
27
|
-
}
|
28
22
|
}
|
23
|
+
}
|
24
|
+
.navigationBarTitle(Text("はじめに"), displayMode: .inline)
|
25
|
+
}
|
26
|
+
.navigationViewStyle(StackNavigationViewStyle())
|
27
|
+
}
|
28
|
+
}
|
29
|
+
}
|
29
30
|
struct MaturiView: View {
|
30
31
|
var body: some View{
|
31
32
|
NavigationLink(destination: MaturiSecondView())
|
32
|
-
|
33
|
+
{
|
33
|
-
|
34
|
+
Text("おまつり")
|
34
|
-
|
35
|
+
.font(.largeTitle)
|
35
|
-
|
36
|
+
.foregroundColor(Color.white)
|
36
|
-
|
37
|
+
.frame(width: 200, height: 100)
|
37
|
-
|
38
|
+
.background(.green) }
|
38
39
|
}
|
39
40
|
struct MaturiSecondView: View{
|
40
41
|
private let maturiSound = try! AVAudioPlayer(data: NSDataAsset(name: "ymaturi")!.data)
|
41
42
|
|
42
43
|
private func playSound(){
|
43
|
-
|
44
|
+
maturiSound.stop()
|
44
|
-
|
45
|
+
maturiSound.currentTime = 0.0
|
45
|
-
|
46
|
+
maturiSound.play()
|
46
|
-
|
47
|
+
}
|
47
48
|
var body: some View {
|
48
|
-
|
49
|
+
Button(action:{
|
49
|
-
|
50
|
+
playSound()
|
50
|
-
|
51
|
+
}) {
|
51
|
-
|
52
|
+
Image("maturi")
|
52
|
-
|
53
|
+
.renderingMode(.original)
|
53
54
|
}
|
54
55
|
}
|
55
56
|
}
|
56
57
|
}
|
57
58
|
struct ContentView_Previews: PreviewProvider {
|
58
|
-
|
59
|
+
static var previews: some View {
|
59
|
-
|
60
|
+
ContentView()
|
60
|
-
|
61
|
+
}
|
61
|
-
}
|
62
|
+
}
|
63
|
+
```
|