質問編集履歴

5

タイトルの修正

2022/06/11 16:06

投稿

uAd30HzDXwVQVwb
uAd30HzDXwVQVwb

スコア4

test CHANGED
@@ -1 +1 @@
1
- カレンダーの不具合につ
1
+ カレンダー日選択時他日の反応を無くしたです。
test CHANGED
File without changes

4

タイトルの変更

2022/06/11 15:07

投稿

uAd30HzDXwVQVwb
uAd30HzDXwVQVwb

スコア4

test CHANGED
@@ -1 +1 @@
1
- カレンダーのフラッシュの消し方
1
+ カレンダーの不具合について
test CHANGED
File without changes

3

タイトルの修正

2022/06/11 13:38

投稿

uAd30HzDXwVQVwb
uAd30HzDXwVQVwb

スコア4

test CHANGED
@@ -1 +1 @@
1
- 構造体配列扱いについて
1
+ カレンダーフラッシュの消し
test CHANGED
File without changes

2

文章とコードの修正

2022/06/11 13:36

投稿

uAd30HzDXwVQVwb
uAd30HzDXwVQVwb

スコア4

test CHANGED
File without changes
test CHANGED
@@ -1,20 +1,12 @@
1
- 下記コードについて2つ質問があります。
1
+ 下記コードについて質問があります。長いコード失礼します。
2
- 最終的に行いたいことは、日を跨いだ予定を追加した場合に予定がある日全てにポイントが付くようにしたいです。
3
- 文字数の関係で省きましたが予定は日を跨いで表示されます。
4
-
5
- ①下記コードでencodeを1箇所、decodeを3箇所行っています。構造体配列のarrayのみのencode,decodeが間違っているようです。(arrayがポイントを担っています。)
6
- 構造体配列に配列が含まれている場合、どのようにencode,decodeをすれば良いでしょうか?
7
- for文の366は適当です。
8
- ※CalendarPageの@State var array:[Date]と構造体配列のarrayは別です。ややこしくてすみません。
9
-
10
- ②現在、カレンダーのにちを押すとの日が一瞬色付くのでが、なぜでしょうか?
2
+ を押すとの日が選択されるのですが、他の日が一瞬色付きま
11
-
12
- コードと面倒くさ質問ですみません
3
+ これを光らなようにしたいです。
13
4
  お力をお貸しいただきたいです。よろしくお願いします。
14
5
 
15
6
  ```swift
16
7
  import SwiftUI
17
8
  import FSCalendar
9
+
18
10
  struct CalendarPage2:View{
19
11
  @State var show: Bool = false
20
12
  @State var selectedDate = Date()
@@ -23,9 +15,12 @@
23
15
  @State var m1 = Date()
24
16
  @State var m2 = Date()
25
17
  @State var sortarrays:[Sortarray]
18
+
19
+
26
20
  var body: some View{
27
21
  ScrollView{
22
+ VStack{
28
- VStack{Calendarpage(selectedDate:$selectedDate,sortarrays:$sortarrays).frame(height:360)
23
+ Calendarpage(selectedDate:$selectedDate,array:$array).frame(height:360)
29
24
  Text(selectedDate,style:.date).padding()
30
25
  Button(action:{self.show = true;m1=selectedDate;m2=selectedDate}){Text("+")}.sheet(isPresented:self.$show){
31
26
  SecondView(show:$show,selectedDate:$selectedDate,array:$array,newstart:$m1,newfinish:$m2,sortarrays:$sortarrays,s:$s)
@@ -69,7 +64,6 @@
69
64
  }
70
65
  }
71
66
  struct Sortarray:Codable{
72
- var array : [Date]
73
67
  var arraytitle : String
74
68
  var arraymemo : String
75
69
  var arraystart : Date
@@ -103,37 +97,14 @@
103
97
  if let savedValue = UserDefaults.standard.data(forKey: "sortkey") {
104
98
  let decoder = JSONDecoder()
105
99
  if let arraysort = try? decoder.decode([Sortarray].self, from: savedValue) {
106
- sortarrays.append(Sortarray(array:arraysort[index].array,arraytitle:arraysort[index].arraytitle,arraymemo:arraysort[index].arraymemo,arraystart:arraysort[index].arraystart,arrayfinish:arraysort[index].arrayfinish))
100
+ sortarrays.append(Sortarray(arraytitle:arraysort[index].arraytitle,arraymemo:arraysort[index].arraymemo,arraystart:arraysort[index].arraystart,arrayfinish:arraysort[index].arrayfinish))
107
101
  }
108
102
  }
109
103
  }
110
104
  }
111
105
  else{
112
106
  }
113
- for index in 0..<366{
114
- if(equals(newstart,newfinish,selectedDate,index)){
115
- newarray.append(selectedDate)
116
- }
117
- else if(notequalfinish(newstart,newfinish,selectedDate,index)){
118
- let calendar = Calendar.current
119
- guard let modifiedDate = calendar.date(byAdding: .day, value: index, to: selectedDate) else {return}
120
- newarray.append(modifiedDate)
121
- }
122
- else if(notequalstart(newstart,newfinish,selectedDate,index)){
123
- let calendar = Calendar.current
124
- guard let modifiedDate = calendar.date(byAdding: .day, value: index, to: selectedDate) else {return}
125
- newarray.append(modifiedDate)
126
- }
127
- else if(newstart<selectedDate && selectedDate<newfinish){
128
- let calendar = Calendar.current
129
- guard let modifiedDate = calendar.date(byAdding: .day, value: index, to: selectedDate) else {return}
130
- newarray.append(modifiedDate)
131
- }
132
- else{
133
- break
134
- }
135
- }
136
- sortarrays.append(Sortarray(array:newarray,arraytitle:newtitle,arraymemo:newmemo,arraystart:newstart,arrayfinish:newfinish))
107
+ sortarrays.append(Sortarray(arraytitle:newtitle,arraymemo:newmemo,arraystart:newstart,arrayfinish:newfinish))
137
108
  sortarrays.sort(by: {$1.arraystart > $0.arraystart})
138
109
  let encoder = JSONEncoder()
139
110
  if let encodedValue = try? encoder.encode(sortarrays) {
@@ -142,50 +113,11 @@
142
113
  array.append(selectedDate)
143
114
  UserDefaults.standard.set(array, forKey: "arraykey")
144
115
  }
145
- func equals(_ date1: Date, _ date2: Date,_ date3: Date,_ index:Int) -> Bool {
146
- let calendar = Calendar.current
147
- guard let modifiedDate = calendar.date(byAdding: .day, value: index, to: date3) else {return (0 != 0)}
148
- let components1 = calendar.dateComponents([.year, .month, .day], from: date1)
149
- let components2 = calendar.dateComponents([.year, .month, .day], from: date2)
150
- let components3 = calendar.dateComponents([.year, .month, .day], from: modifiedDate)
151
- if(components1 == components2 && components2 == components3){
152
- return true
153
- }
154
- else{
155
- return false
156
- }
157
- }
158
- func notequalfinish(_ date1: Date, _ date2: Date,_ date3: Date,_ index: Int) -> Bool {
159
- let calendar = Calendar.current
160
- guard let modifiedDate = calendar.date(byAdding: .day, value: index, to: date3) else {return (0 != 0)}
161
- let components1 = calendar.dateComponents([.year, .month, .day], from: date1)
162
- let components2 = calendar.dateComponents([.year, .month, .day], from: date2)
163
- let components3 = calendar.dateComponents([.year, .month, .day], from: modifiedDate)
164
- if(components1 == components3 && components2 != components3){
165
- return true
166
- }
167
- else{
168
- return false
169
- }
170
- }
171
- func notequalstart(_ date1: Date, _ date2: Date,_ date3: Date,_ index: Int) -> Bool {
172
- let calendar = Calendar.current
173
- guard let modifiedDate = calendar.date(byAdding: .day, value: index, to: date3) else {return(0 != 0)}
174
- let components1 = calendar.dateComponents([.year, .month, .day], from: date1)
175
- let components2 = calendar.dateComponents([.year, .month, .day], from: date2)
176
- let components3 = calendar.dateComponents([.year, .month, .day], from: modifiedDate)
177
- if(components1 != components3 && components2 == components3){
178
- return true
179
- }
180
- else{
181
- return false
182
- }
183
- }
184
116
  }
185
117
 
186
118
  struct Calendarpage: UIViewRepresentable {
187
119
  @Binding var selectedDate:Date
188
- @Binding var sortarrays:[Sortarray]
120
+ @Binding var array:[Date]
189
121
 
190
122
 
191
123
  func makeUIView(context: Context) -> UIView {
@@ -197,22 +129,22 @@
197
129
  }
198
130
  func updateUIView(_ uiView: UIView, context: Context) {
199
131
  guard let v = uiView as? FSCalendar else{return}
200
- context.coordinator.sortarrays = sortarrays
132
+ context.coordinator.array = array
201
133
  v.reloadData()
202
134
  }
203
135
  func makeCoordinator() -> Coordinator {
204
- return Coordinator(self,sortarrays:sortarrays)
136
+ return Coordinator(self,array:array)
205
137
  }
206
138
 
207
139
 
208
140
  class Coordinator: NSObject,FSCalendarDataSource, FSCalendarDelegate{
209
- var sortarrays : [Sortarray]
141
+ var array:[Date]
210
142
  var parent : Calendarpage
211
143
 
212
144
 
213
- init(_ parent: Calendarpage,sortarrays:[Sortarray]) {
145
+ init(_ parent: Calendarpage,array:[Date]) {
214
146
  self.parent = parent
215
- self.sortarrays = sortarrays
147
+ self.array = array
216
148
  }
217
149
 
218
150
 
@@ -220,17 +152,11 @@
220
152
  parent.selectedDate = date
221
153
  }
222
154
  func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
223
- if let savedValue = UserDefaults.standard.data(forKey: "sortkey") {
224
- let decoder = JSONDecoder()
225
- if let arraysort = try? decoder.decode([Sortarray].self, from: savedValue) {
226
- for n in 0..<arraysort.count{
227
- if arraysort[n].array.contains(date){
155
+ if array.contains(date) {
228
- return 1
156
+ return 1
229
- }
157
+ } else {
230
- }
158
+ return 0
231
- }
232
159
  }
233
- return 0
234
160
  }
235
161
  }
236
162
  }
@@ -242,4 +168,5 @@
242
168
  }
243
169
  }
244
170
  }
171
+
245
172
  ```

1

コードの修正

2022/06/11 06:52

投稿

uAd30HzDXwVQVwb
uAd30HzDXwVQVwb

スコア4

test CHANGED
File without changes
test CHANGED
@@ -4,6 +4,7 @@
4
4
 
5
5
  ①下記コードでencodeを1箇所、decodeを3箇所行っています。構造体配列のarrayのみのencode,decodeが間違っているようです。(arrayがポイントを担っています。)
6
6
  構造体配列に配列が含まれている場合、どのようにencode,decodeをすれば良いでしょうか?
7
+ for文の366は適当です。
7
8
  ※CalendarPageの@State var array:[Date]と構造体配列のarrayは別です。ややこしくてすみません。
8
9
 
9
10
  ②現在、カレンダーの日にちを押すと別の日が一瞬色付くのですが、なぜでしょうか?
@@ -14,28 +15,20 @@
14
15
  ```swift
15
16
  import SwiftUI
16
17
  import FSCalendar
17
-
18
18
  struct CalendarPage2:View{
19
19
  @State var show: Bool = false
20
20
  @State var selectedDate = Date()
21
21
  @State var array : [Date]
22
22
  @State var s = 0
23
- @State var m = 0
24
23
  @State var m1 = Date()
25
24
  @State var m2 = Date()
26
25
  @State var sortarrays:[Sortarray]
27
-
28
-
29
26
  var body: some View{
30
27
  ScrollView{
31
- VStack{
32
- Calendarpage(selectedDate:$selectedDate,sortarrays:$sortarrays).frame(height:360)
28
+ VStack{Calendarpage(selectedDate:$selectedDate,sortarrays:$sortarrays).frame(height:360)
33
- HStack{
34
- Text(selectedDate,style:.date).padding()
29
+ Text(selectedDate,style:.date).padding()
35
- Spacer()
36
- Button(action:{self.show = true;m1=selectedDate;m2=selectedDate}){Text("+")}.sheet(isPresented:self.$show){
30
+ Button(action:{self.show = true;m1=selectedDate;m2=selectedDate}){Text("+")}.sheet(isPresented:self.$show){
37
- SecondView(show:$show,selectedDate:$selectedDate,array:$array,newstart:$m1,newfinish:$m2,sortarrays:$sortarrays,s:$s)
31
+ SecondView(show:$show,selectedDate:$selectedDate,array:$array,newstart:$m1,newfinish:$m2,sortarrays:$sortarrays,s:$s)
38
- }
39
32
  }
40
33
  if(array.count == 0){
41
34
  }