簡體   English   中英

Swift獲取當前顯示的索引UICollectionView

[英]Swift get current displayed index UICollectionView

我正在為iOS做音樂播放器。 我有音樂列表和詳細視圖,您可以左右滑動來更改歌曲。 問題是我無法使當前顯示的元素播放適當的歌曲,但是顯示的圖像很好。 我想知道如何在“輪播視圖”中獲取當前元素,因為cellForItemAt不起作用,當我向右滑動時,它會給我2個下一個索引。

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) {

}  
}

要在集合視圖中獲取當前顯示的單元格,可以訪問UICollectionViewvisibleCells屬性。 文檔

您也可以使用indexPathsForVisibleItems來獲取indexPaths。 文檔

然后,還有indexPath(for:)來獲取您具有引用的單元格的indexPath,例如從visibleCells的結果中。 文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM