首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVPlayerViewController in UICollectionViewCell bug?

AVPlayerViewController in UICollectionViewCell bug?
EN

Stack Overflow用户
提问于 2015-11-26 00:01:07
回答 1查看 3.6K关注 0票数 1

在我的UICollectionView里,我有一个手机。这是我的cellForItemAtIndexPath

代码语言:javascript
复制
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    var videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellVideo", forIndexPath: indexPath) as? CollectionViewCell

    let post = self.arrayOfDetails[indexPath.row]

    var myUrl = post.image // post.image is image url

    let fileUrl = NSURL(string: post.image)

    aPlayer = AVPlayer(URL: fileUrl)

    moviePlayerController.player = aPlayer
    moviePlayerController.view.frame = CGRectMake(8, 8, videoCell!.frame.size.width-16, 310)
    moviePlayerController.videoGravity = AVLayerVideoGravityResizeAspectFill
    moviePlayerController.view.sizeToFit()
    moviePlayerController.showsPlaybackControls = true
    videoCell!.addSubview(moviePlayerController.view)

    return videoCell!
}

问题是,AVPlayerViewController并不会出现在每个单元上。但是只有在我所看到的单元格下面,所以如果我在UICollectionView中向下滚动,一旦一个新的单元格出现在屏幕上,AVPlayerViewController就在该单元格上,而不是屏幕中部的单元格上。有什么建议吗?我可能会错在这里吗?

视频:https://vid.me/e/fU5X

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-26 00:17:08

每个单元格都需要一个新的aPlayer和moviePlayerController,其中包含视频。像这样的事情应该可以做到:

代码语言:javascript
复制
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        var videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellVideo", forIndexPath: indexPath) as? CollectionViewCell

        var aPlayer = AVPlayer()
        let moviePlayerController = AVPlayerViewController()

        let post = self.arrayOfDetails[indexPath.row]

        var myUrl = post.image // post.image is image url

        let fileUrl = NSURL(string: post.image)

        aPlayer = AVPlayer(URL: fileUrl)

        moviePlayerController.player = aPlayer
        moviePlayerController.view.frame = CGRectMake(8, 8, videoCell!.frame.size.width-16, 310)
        moviePlayerController.videoGravity = AVLayerVideoGravityResizeAspectFill
        moviePlayerController.view.sizeToFit()
        moviePlayerController.showsPlaybackControls = true
        videoCell!.addSubview(moviePlayerController.view)

        return videoCell!
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33928536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档