从嵌入在同一viewController中的不同集合视图的两个单元格之间发送数据

时间:2017-03-31 11:53:13

标签: ios swift uicollectionview uislider

我在同一个viewController中嵌入了两个collectionView,在每个collectionView中我添加了一个控制AVAudioplayer音量的UISlider。我需要的是当我更改两个滑块之一的值时,另一个滑块控制相同的AVAudioPlayer。

修改

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    switch collectionView {
    case mainCollectionView:
        let cell = mainCollectionView.dequeueReusableCell(withReuseIdentifier: "mainCell", for: indexPath) as! MainCell
        if cell.isSelected {
            cell.backgroundColor = UIColor.magenta
            cell.audioPlayer = indexOfAudioPlayer[indexPath]
            cell.volumeSlider.isHidden = false
        } else {
            cell.backgroundColor = colorArray[indexPath.section]
            cell.volumeSlider.isHidden = true
        }
        return cell
    case soundVolumeCollectionView:
        let cell = soundVolumeCollectionView.dequeueReusableCell(withReuseIdentifier: "soundVolumeCell", for: indexPath) as! MainCell
        cell.audioPlayer = sortedAudioPlayers[indexPath.item]
        return cell
    default:
        let cell = menuCollectionView.dequeueReusableCell(withReuseIdentifier: "menuCell", for: indexPath) as! MenuCell
        cell.backgroundColor = colorArray[indexPath.item]
        return cell
    }
}

mainCollectionView和soundVolumeCollectionView都负责控制AVAudioPlayers。

1 个答案:

答案 0 :(得分:0)

您应该使用NotificationCenter。这并不是最终的代码,但它应该让你朝着正确的方向前进。您的MenuCell课程需要实施类似的观察员并发送类似的通知。

class ViewController: UIViewController, UICollectionViewDataSource {
    var volume: Float {
        didSet {
            player.volume = volume
            NotificationCenter.default.post(name: Notification.Name.playerVolumeChanged, object: self, userInfo: ["volume":volume]);
        }
    }

    var player: AVAudioPlayer

    override init() {
        super.init()
        NotificationCenter.default.addObserver(self, selector: #selector(volumeChanged(_:)), name: Notification.Name.sliderChangedVolume, object: nil)
    }

    func volumeChanged(_ notification:Notification) {
        if let userInfo = notification.userInfo, let newVolume = userInfo["volume"] as? Float {
            self.volume = newVolume
        }
    }
}

extension Notification.Name {
    static let playerVolumeChanged = Notification.Name("reverse.domain.name.playerVolumeChanged")
    static let sliderChangedVolume = Notification.Name("reverse.domain.name.sliderChangedVolume")
}

class MainCell: UICollectionViewCell {

    var slider = UISlider()

    required init(frame: CGRect) {
        super.init(frame: frame)
        NotificationCenter.default.addObserver(self, selector: #selector(volumeDidChange(_:)), name: Notification.Name.playerVolumeChanged, object: nil)
        slider.addTarget(self, action: #selector(sliderChanged(_:)), for: UIControlEvents.valueChanged);
    }

    func volumeDidChange(_ notification:Notification) {
        if let userInfo = notification.userInfo, let newVolume = userInfo["volume"] as? Float {
            self.slider.value = newVolume
        }
    }

    func sliderChanged(_ slider:UISlider) {
        NotificationCenter.default.post(name: Notification.Name.sliderChangedVolume, object: self, userInfo: ["volume": slider.value])
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        NotificationCenter.default.removeObserver(self, name: Notification.Name.playerVolumeChanged, object: nil)
    }
}