Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift获取当前显示的索引UICollectionView_Ios_Swift_Uicollectionview_Carousel - Fatal编程技术网

Ios Swift获取当前显示的索引UICollectionView

Ios Swift获取当前显示的索引UICollectionView,ios,swift,uicollectionview,carousel,Ios,Swift,Uicollectionview,Carousel,我正在为ios制作一个音乐播放器。我有一个音乐列表和一个详细视图,你可以左右滑动来更改歌曲。问题是我无法让当前显示的元素播放适当的歌曲,但显示的图像很好。我想知道如何在Carousel视图中获取当前元素,因为cellForItemAt不工作,当我向右滑动时,它会给我接下来的两个索引 import UIKit import AVFoundation private let reuseIdentifier = "Cell" class PhotoCarouselCollectionViewCo

我正在为ios制作一个音乐播放器。我有一个音乐列表和一个详细视图,你可以左右滑动来更改歌曲。问题是我无法让当前显示的元素播放适当的歌曲,但显示的图像很好。我想知道如何在Carousel视图中获取当前元素,因为cellForItemAt不工作,当我向右滑动时,它会给我接下来的两个索引

import UIKit
import AVFoundation

private let reuseIdentifier = "Cell"


class PhotoCarouselCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet var collectionView: UICollectionView!

@IBAction func pause(_ sender: Any) {
    if audioPlayer.isPlaying == true{
        audioPlayer.pause()
    }
}
@IBAction func play(_ sender: Any) {
    if audioPlayer.isPlaying == false{
        audioPlayer.play()
    }
}

@IBAction func slider(_ sender: UISlider) {
    audioPlayer.volume = sender.value
}

private var photoSet = [
    Photo(name: "pendulum", song: "pendulum"),
    Photo(name: "martin", song: "doitright"),
    Photo(name: "kungs",song: "thisgirl"),
    Photo(name: "future",song: "future"),
]

var photo: Photo?
var currentPhoto: Int?

let scrollView = UICollectionView.self
var pageControl : UIPageControl = UIPageControl(frame: CGRect(x:50,y: 300, width:200, height:50))



override func viewDidLoad() {

    super.viewDidLoad()

    let swipeRight = UISwipeGestureRecognizer(target: self, action: Selector(("respondToSwipeGesture:")))
    swipeRight.direction = UISwipeGestureRecognizerDirection.right
    self.view.addGestureRecognizer(swipeRight)

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: Selector(("respondToSwipeGesture:")))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.left
    self.view.addGestureRecognizer(swipeLeft)

}

func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {


        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            print("Swiped right")
        case UISwipeGestureRecognizerDirection.down:
            print("Swiped down")
        case UISwipeGestureRecognizerDirection.left:
            print("Swiped left")
        case UISwipeGestureRecognizerDirection.up:
            print("Swiped up")
        default:
            break
        }


    }

    //pageControl.currentPage = {currentPhoto!}();


    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
    // self.collectionView!.register(PhotoCarouselCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    // Do any additional setup after loading the view.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func numberOfSections(in collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return photoSet.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PhotoCarouselCollectionViewCell

    // Configure the cell
    let photo = photoSet[indexPath.row.advanced(by: currentPhoto!)]

    print(indexPath.row)
    //let photo =  photoSet[currentPhoto!]
    //let photo = photoSet[indexPath.row]\

 //GETTING THE MUSIC TO BE PLAYED
 do{
        let audioPath = Bundle.main.path(forResource: photoSet[indexPath.row].song, ofType: "mp3")
        try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
        audioPlayer.play()
    }
    catch{
        print ("ERROR")
    }


    cell.imageView.image = UIImage(named: photo.name)

    return cell
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

}  
}

要获取集合视图中当前显示的单元格,可以访问
UICollectionView
visibleCells
属性

您也可以使用
indexPathsForVisibleItems
来获取indexPaths

然后,还有
indepath(for:)
来获取有引用的单元格的indepath,比如从
visibleCells
的结果中