[英]Set video on old page to 0.0 seconds when scrolled to new page
I have a scrollView
with pagingEnabled
set to true
. 我有一个scrollView
pagingEnabled
设置为true
pagingEnabled
。 Each of these pages contains one video covering the entire screen. 这些页面中的每个页面都包含一个覆盖整个屏幕的视频。 The videos are added using a for
loop and a class called PlayerView
(not only adding the video but also information about the video). 使用for
循环和名为PlayerView
的类添加视频(不仅添加视频,而且包含有关视频的信息)。 Within the PlayerView
class I have a function which starts/stops playing the video in the respective page and all that works fine. 在PlayerView
类中,我有一个功能,可以开始/停止在相应页面中播放视频,并且一切正常。
My problem is I would like to set the video to the start of it and stop it there (at 0.0 seconds so to say) whenever the page containing that video is not visible anymore (with self.player?.seek(to: kCMTimeZero)
and self.player?.pause()
). 我的问题是,无论何时不再包含该视频的页面(使用self.player?.seek(to: kCMTimeZero)
我都希望将视频设置为开始并在此处停止播放(可以说是0.0秒self.player?.seek(to: kCMTimeZero)
和self.player?.pause()
)。
In my scrollView
I have a function which detects what page has appeared and also states the previous page when the user scrolls from one page to another (in form of an index, eg when scrolled from page 2 to page 3 the function will print: "1 2"). 在我的scrollView
我有一个功能可以检测到出现了什么页面,并且还可以在用户从一个页面滚动到另一页面时声明上一页(以索引的形式,例如,从第2页滚动到第3页时,该函数将打印: 1 2“)。 To stick to the example: I would like to set the video on page 2 (index: 1) to 0 and pause it. 坚持以下示例:我想将第2页(索引:1)上的视频设置为0并暂停它。
class HomeController: UIViewController, UIScrollViewDelegate
) 创建scrollView(在class HomeController: UIViewController, UIScrollViewDelegate
) func initiateScrollView() {
//create scrollView with paging enabled
let scrollView = UIScrollView(frame: view.bounds)
scrollView.isPagingEnabled = true
scrollView.delegate = self
view.addSubview(scrollView)
//get page size
let pageSize = view.bounds.size
//three simple UIView are created (view1, view2, view3)
//array with individual views
let pagesViews = [view1, view2, view3]
//amount of views
let numberOfPages = pagesViews.count
//add subviews (pages)
for (pageIndex, page) in pagesViews.enumerated(){
//add individual pages to scrollView
page.frame = CGRect(origin: CGPoint(x:0 , y: CGFloat(pageIndex) * pageSize.height), size: pageSize)
scrollView.addSubview(page)
//add Subview with videoplayer frame to individual "pages"
let videoPlayerFrame = CGRect(x: 0, y: 0, width: pageSize.width, height: pageSize.height)
let videoPlayerView = PlayerView(frame: videoPlayerFrame)
page.addSubview(videoPlayerView)
}
//adjust size of scrollView
scrollView.contentSize = CGSize(width: pageSize.width, height: pageSize.height * CGFloat(numberOfPages))
}
Do you have an idea how to pause the video on the previous page? 您是否有办法暂停上一页中的视频? I would really appreciate your help! 我将衷心感谢您的帮助!
There are about two ways I think you could do this depending on how exactly you want the video to stop. 我认为您可以通过两种方式来完成此操作,具体取决于您希望视频停止播放的程度。
Method 1: The Video on the Previous View Stops and the Video on the New View Does Not Auto-Play 方法1:上一个视图上的视频停止并且新视图上的视频不能自动播放
You can create an Notification observer in each of the views which listens for a change of scroll view and fires a common function to stop the video (it is good practice to create a protocol to constrain these views to the common function although I will not do it here). 您可以在每个视图中创建一个Notification观察器,以侦听滚动视图的变化并触发通用功能来停止视频(创建协议以将这些视图限制为通用功能是一种很好的做法,尽管我不会这样做在这里)。 So in each of the views you add an observer like this in the ViewDidLoad: 因此,在每个视图中,您都需要在ViewDidLoad中添加这样的观察者:
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.stopVideo), name: NSNotification.Name(rawValue: "stopVideo"), object: nil)
And you add the corresponding function that will be called in the view: 然后添加将在视图中调用的相应函数:
func stopVideo() {
self.player.stop()
}
Then in your scrollview, in the function you mentioned that "detects what page has appeared and also states the previous page", you fire a notification whenever the scroll view changes: 然后在您的滚动视图中,在您提到的“检测到出现了什么页面并还声明了上一页”的函数中,只要滚动视图发生更改,便会触发通知:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "stopVideo"), object: nil)
This will stop any video playing in all the views whenever there is a change of view. 每当视图改变时,这将停止在所有视图中播放任何视频。 PS Also remember to remove the observer in the deinit() PS还记得在deinit()中删除观察者
Method 2: The Video on the Previous View Stops and the Video on the New View Does Will Auto-Play 方法2:上一个视图上的视频停止并且新视图上的视频会自动播放
If the previous video will stop and new video will auto-play you could make a protocol for each of the views that they must adhere to like so: 如果上一个视频将停止并且新视频将自动播放,则可以为它们必须遵守的每个视图制定协议,如下所示:
protocol StopVideoDelegate: class {
func stopVideo()
func startVideo()
}
Each of your views must implement this protocol, for example: 您的每个视图都必须实现此协议,例如:
class ViewController1InScrollView: StopVideoDelegate {
func stopVideo() {
self.playerLayer.stop()
}
func startVideo() {
self.playerLayer.start()
}
}
Then in your scrollview, in the function you mentioned that "detects what page has appeared and also states the previous page", can simply cast the previous view and next view to the protocol type and call the corresponding function: 然后,在您的滚动视图中,您提到的功能“检测到出现了什么页面并还声明了上一页”,可以简单地将上一个视图和下一个视图转换为协议类型,然后调用相应的函数:
func theFunctionThatDetectsViewChangeInScrollView() {
nextView as! StopVideoDelegate
nextView.startVideo()
previousView as! StopVideoDelegate
previousView.stopVideo()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.