質問編集履歴

2

コード抜け

2017/04/20 09:06

投稿

Ethan
Ethan

スコア8

test CHANGED
File without changes
test CHANGED
@@ -232,6 +232,8 @@
232
232
 
233
233
  sender.isSelected = false
234
234
 
235
+ sender.setImage(UIImage(named: "play"), for: UIControlState.normal)
236
+
235
237
  }
236
238
 
237
239
  else{

1

修正点の追加

2017/04/20 09:06

投稿

Ethan
Ethan

スコア8

test CHANGED
File without changes
test CHANGED
@@ -178,7 +178,91 @@
178
178
 
179
179
  ###試したこと
180
180
 
181
+
182
+
183
+ ```
184
+
185
+ func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
186
+
187
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Storyboard.CellIdentifier, for: indexPath) as! AudioCollectionViewCell
188
+
189
+ cell.playButton.tag = indexPath.row
190
+
191
+
192
+
193
+ if let optionalIndexPath = currentSelectedIndexPath{
194
+
195
+ if (indexPath == optionalIndexPath){
196
+
197
+ cell.playButton.setImage(UIImage(named: "stop"), for: UIControlState.selected)
198
+
199
+ }
200
+
201
+ else{
202
+
203
+ cell.playButton.setImage(UIImage(named: "play"), for: UIControlState.normal)
204
+
205
+ }
206
+
207
+ }
208
+
209
+ cell.playButton.addTarget(self,action:#selector(playButtonDidTouch(sender:)), for: .touchUpInside)
210
+
211
+ return cell
212
+
213
+ }
214
+
215
+
216
+
217
+ func playButtonDidTouch(sender:UIButton) {
218
+
219
+
220
+
221
+ let cell = sender.superview?.superview as! UICollectionViewCell
222
+
181
- ・Collection Viewの再利用対策もしましたが、上手く動作しませんでした。
223
+ let indexPath = collectionView.indexPath(for: cell)!
224
+
225
+ currentSelectedIndexPath = indexPath
226
+
227
+
228
+
229
+ if (sender.isSelected){
230
+
231
+ mediaPlayer.pause()
232
+
233
+ sender.isSelected = false
234
+
235
+ }
236
+
237
+ else{
238
+
239
+ let row = sender.tag
240
+
241
+ let url = ""
242
+
243
+ do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)}
244
+
245
+ catch {print("エラー")}
246
+
247
+ let playerItem = AVPlayerItem( url:NSURL( string:url! )! as URL )
248
+
249
+ mediaPlayer = AVPlayer(playerItem:playerItem)
250
+
251
+ mediaPlayer.rate = 1.0
252
+
253
+ mediaPlayer.play()
254
+
255
+ sender.isSelected = true
256
+
257
+ sender.setImage(UIImage(named: "stop"), for: UIControlState.normal)
258
+
259
+ }
260
+
261
+ }
262
+
263
+ ```
264
+
265
+
182
266
 
183
267
 
184
268