在我的UICollectionView里,我有一个手机。这是我的cellForItemAtIndexPath
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就在该单元格上,而不是屏幕中部的单元格上。有什么建议吗?我可能会错在这里吗?
发布于 2015-11-26 00:17:08
每个单元格都需要一个新的aPlayer和moviePlayerController,其中包含视频。像这样的事情应该可以做到:
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!
}https://stackoverflow.com/questions/33928536
复制相似问题