我是快速开发(和一般编码)的新手,我正在尝试开发一个表视图控制器,它将在视频完全显示时显示视频和播放。我可以播放视频,但我真的希望能够加载所有视频并暂停那些不完全可见的视频,而不是在滚动时重新加载视频。
此时,我有一个表视图类,其中包含一个播放视频的方法,并检查scrollViewDidScroll中的可见单元格。在scrollViewDidScroll中,我有一系列if语句,我实现它们以响应可见单元格的数量。
TableViewCell.swift:
@IBOutlet weak var movieView:UIView!
var moviePlayer:MPMoviePlayerController!
var videoURL:NSURL!
override func awakeFromNib() {
super.awakeFromNib()
//initialize movie player
moviePlayer = MPMoviePlayerController(contentURL: videoURL)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
//Action to load video
func displayVideo() {
moviePlayer = MPMoviePlayerController(contentURL: videoURL)
moviePlayer.controlStyle = MPMovieControlStyle.None
moviePlayer.scalingMode = MPMovieScalingMode.Fill
moviePlayer.movieSourceType = MPMovieSourceType.File
moviePlayer.repeatMode = MPMovieRepeatMode.One
moviePlayer.initialPlaybackTime = -1.0
moviePlayer.view.frame = movieView.bounds
moviePlayer.view.center = CGPointMake(CGRectGetMidX(movieView.bounds), CGRectGetMidY(movieView.bounds))
movieView.addSubview(moviePlayer.view)
moviePlayer.prepareToPlay()
moviePlayer.play()
}
TableViewController.swift:
var objects = [[String: String]]()
var moviePlayer:MPMoviePlayerController!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//Number of table view sections
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
//Number of table view rows
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.objects.count
}
override func scrollViewDidScroll(scrollView: UIScrollView) {
let indexPaths = tableView.indexPathsForVisibleRows
if indexPaths?.count == 1 {
let cell1:TableViewCell = tableView.cellForRowAtIndexPath(indexPaths![0] as NSIndexPath) as! TableViewCell
var cellRect : CGRect = self.tableView.rectForRowAtIndexPath(indexPaths![0])
cellRect = self.tableView.superview!.convertRect(cellRect, fromView: self.tableView)
if self.tableView.frame.contains(cellRect) {
if cell1.moviePlayer.playbackState == MPMoviePlaybackState.Playing {
print("keep playing")
}else{
cell1.videoURL = NSURL(string: self.objects[indexPaths![0].row]["storyLink"]!)!
cell1.displayVideo()
}
}
}else if indexPaths?.count == 2 {
let cell1:TableViewCell = tableView.cellForRowAtIndexPath(indexPaths![0] as NSIndexPath) as! TableViewCell
let cell2:TableViewCell = tableView.cellForRowAtIndexPath(indexPaths![1] as NSIndexPath) as! TableViewCell
var cellRect1 : CGRect = self.tableView.rectForRowAtIndexPath(indexPaths![0])
cellRect1 = self.tableView.superview!.convertRect(cellRect1, fromView: self.tableView)
var cellRect2 : CGRect = self.tableView.rectForRowAtIndexPath(indexPaths![1])
cellRect2 = self.tableView.superview!.convertRect(cellRect2, fromView: self.tableView)
if self.tableView.frame.contains(cellRect1) {
if cell1.moviePlayer.playbackState == MPMoviePlaybackState.Playing {
print("keep playing")
}else{
cell1.videoURL = NSURL(string: self.objects[indexPaths![0].row]["storyLink"]!)!
cell1.displayVideo()
}
} else if self.tableView.frame.contains(cellRect2) {
if cell2.moviePlayer.playbackState == MPMoviePlaybackState.Playing {
print("keep playing")
} else {
cell2.videoURL = NSURL(string: self.objects[indexPaths![1].row]["storyLink"]!)!
cell2.displayVideo()
}
}
} else if indexPaths?.count > 2 {
let cell1:TableViewCell = tableView.cellForRowAtIndexPath(indexPaths![0] as NSIndexPath) as! TableViewCell
let cell2:TableViewCell = tableView.cellForRowAtIndexPath(indexPaths![1] as NSIndexPath) as! TableViewCell
let cell3:TableViewCell = tableView.cellForRowAtIndexPath(indexPaths![2] as NSIndexPath) as! TableViewCell
var cellRect1 : CGRect = self.tableView.rectForRowAtIndexPath(indexPaths![0])
cellRect1 = self.tableView.superview!.convertRect(cellRect1, fromView: self.tableView)
var cellRect2 : CGRect = self.tableView.rectForRowAtIndexPath(indexPaths![1])
cellRect2 = self.tableView.superview!.convertRect(cellRect2, fromView: self.tableView)
var cellRect3 : CGRect = self.tableView.rectForRowAtIndexPath(indexPaths![2])
cellRect3 = self.tableView.superview!.convertRect(cellRect3, fromView: self.tableView)
if self.tableView.frame.contains(cellRect1) {
if cell1.moviePlayer.playbackState == MPMoviePlaybackState.Playing {
print("keep playing")
}else{
cell1.videoURL = NSURL(string: self.objects[indexPaths![0].row]["storyLink"]!)!
cell1.displayVideo()
}
} else if self.tableView.frame.contains(cellRect2) {
if cell2.moviePlayer.playbackState == MPMoviePlaybackState.Playing {
print("keep playing")
} else {
cell2.videoURL = NSURL(string: self.objects[indexPaths![1].row]["storyLink"]!)!
cell2.displayVideo()
}
} else if self.tableView.frame.contains(cellRect2) {
if cell3.moviePlayer.playbackState == MPMoviePlaybackState.Playing {
print("keep playing")
} else {
cell3.videoURL = NSURL(string: self.objects[indexPaths![2].row]["storyLink"]!)!
cell3.displayVideo()
}
}
}
}
代码可以工作,但实际上并不是我想要的。我真的想在滚动时暂停视频,并且可以看到新的单元格。
我确信有更好的方法来实现这一目标,但正如我之前提到的那样,我是一个新手,并且正在将这些内容与我在网上找到的东西拼凑在一起。
任何帮助都非常感激。