我有一个2维UIPageViewController(两个PageViewController,一个水平,一个垂直)。 在第一级(X = 0),我显示带有自动播放的全屏视频。当我使用过渡样式Page Curl时,我没有任何问题,但是使用过渡样式Scroll,事实上“后视图控制器”被预加载,它将执行代码并自动播放视频(托管在youtube上),因此,它将停止我实际看到的视频控制器上的视频,而且,我将在后台听到该视频。
我无法确定如何确保正在播放的视频是我真正关注的视频。
这就是我设置观点的方式:
导入UIKit
类YViewController:UIViewController {
var yPageIndex: Int!
var xPageIndex: Int!
var yLabelText: String!
override func viewDidLoad() {
super.viewDidLoad()
if (xPageIndex == 0) { // if xPageIndex == 0 then you are on the tag page. Else, you are browsing
// HOME SCREEN
let homeLabel = UILabel(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
// homeLabel.backgroundColor = UIColor.blueColor()
// homeLabel.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2)
// homeLabel.textAlignment = NSTextAlignment.Center
homeLabel.numberOfLines = 0
let htmlString = "<center><h1>Welcome John Smith</h1> <h2> Your Tags: </h2> <i> #unity #iOS #php #XCODE</i><br><br><br><br><br><br> <i>Swipe left to start browsing job offers</i></center>"
let encodedData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
do {
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
homeLabel.attributedText = attributedString
} catch _ {
print("Cannot create attributed String")
}
self.view.addSubview(homeLabel)
} else {
if yPageIndex == 0 { // if yPageIndex == 0 then you are on video page. Else, you are on the job description page
var webView = UIWebView(frame: self.view.frame)
view.addSubview(webView)
view.bringSubviewToFront(webView)
webView.allowsInlineMediaPlayback = true
webView.mediaPlaybackRequiresUserAction = false
webView.scrollView.bounces = false
let videoID = mainInstance.jobInfoArray[xPageIndex].videoId
let embededHTML = "<html><body style='margin:0px;padding:0px;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}</script><iframe id='playerId' type='text/html' width='\(self.view.frame.size.width)' height='\(self.view.frame.size.height)' src='http://www.youtube.com/embed/\(videoID)?enablejsapi=1&rel=0&playsinline=1&autoplay=0&controls=0' frameborder='0'></body></html>"
webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().resourceURL)
print("width='\(self.view.frame.size.width)' height='\(self.view.frame.size.height)'")
} else {
print("Job " + mainInstance.jobInfoArray[xPageIndex].jobTitle)
print("width='\(self.view.frame.size.width)' height='\(self.view.frame.size.height)'")
var homeLabel = UILabel(frame: CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height))
homeLabel.numberOfLines = 0
let htmlString = "<h1>\(mainInstance.jobInfoArray[xPageIndex].jobTitle)</h1> <h2>Company: \(mainInstance.jobInfoArray[xPageIndex].jobCompany) </h2> <i> tags: \(mainInstance.jobInfoArray[xPageIndex].jobKeyWords)</i><br>Salary: \(mainInstance.jobInfoArray[xPageIndex].jobSalary)<br> <br> \(mainInstance.jobInfoArray[xPageIndex].jobDescription)"
let encodedData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
do {
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
homeLabel.attributedText = attributedString
} catch _ {
print("Cannot create attributed String")
}
homeLabel.sizeToFit()
self.view.addSubview(homeLabel)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
以及我如何配置我的UIPageView
class XViewController:UIViewController,UIPageViewControllerDataSource {
// to init y display
var yPageViewController: UIPageViewController!
var yLabel: NSArray!
// To remove ?
var xPageIndex: Int!
var videoTitleText: String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// self.videoLabel.text = self.videoTitleText
self.yLabel = NSArray(objects: "Video", "Job Description")
self.yPageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("yPageViewController") as! UIPageViewController
self.yPageViewController.dataSource = self
let startVC = self.viewControllerAtIndex(0) as YViewController
let viewControllers = NSArray(object: startVC)
self.yPageViewController.setViewControllers(viewControllers as! [UIViewController], direction: .Forward, animated: false, completion: nil)
self.yPageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width , self.view.frame.size.height)
self.addChildViewController(self.yPageViewController)
self.view.addSubview(self.yPageViewController.view)
self.yPageViewController.didMoveToParentViewController(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewControllerAtIndex(yIndex: Int) -> YViewController
{
if ( (self.yLabel.count == 0) || (yIndex >= self.yLabel.count) ) {
return YViewController()
}
let vc: YViewController = self.storyboard?.instantiateViewControllerWithIdentifier("yContentViewController") as! YViewController
vc.yPageIndex = yIndex
vc.xPageIndex = xPageIndex
vc.yLabelText = self.yLabel[yIndex] as! String
return vc
}
// MARK: Page View Controller Data Source
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let vc = viewController as! YViewController
var yIndex = vc.yPageIndex as Int
if ( (yIndex == 0) || (yIndex == NSNotFound) ) {
return nil
}
yIndex--
return self.viewControllerAtIndex(yIndex)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let vc = viewController as! YViewController
var yIndex = vc.yPageIndex as Int
if (yIndex == NSNotFound || xPageIndex == 0) {
return nil
}
yIndex++
if (yIndex == self.yLabel.count)
{
return nil
}
return self.viewControllerAtIndex(yIndex)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
以防万一,这里的ViewConytoller(它横向和纵向的事实可能会让你有点难以理解。
导入UIKit
类ViewController:UIViewController,UIPageViewControllerDataSource {
var xPageViewController: UIPageViewController!
var videoId: NSArray!
// var yPageViewController:UIPageViewController! // var jobId:NSArray!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.videoId = NSArray(objects: "Video #1", "Video #2", "Video #3", "Video #4", "Video #5")
self.xPageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("xPageViewController") as! UIPageViewController
self.xPageViewController.dataSource = self
let startVC = self.viewControllerAtIndex(0) as XViewController
let viewControllers = NSArray(object: startVC)
self.xPageViewController.setViewControllers(viewControllers as! [UIViewController], direction: .Forward, animated: false, completion: nil)
self.xPageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width , self.view.frame.size.height)
self.addChildViewController(self.xPageViewController)
self.view.addSubview(self.xPageViewController.view)
self.xPageViewController.didMoveToParentViewController(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewControllerAtIndex(xIndex: Int) -> XViewController
{
if ( (self.videoId.count == 0) || (xIndex >= self.videoId.count) ) {
return XViewController()
}
let vc: XViewController = self.storyboard?.instantiateViewControllerWithIdentifier("xContentViewController") as! XViewController
vc.xPageIndex = xIndex
vc.videoTitleText = self.videoId[xIndex] as! String
return vc
}
// MARK: Page View Controller Data Source
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let vc = viewController as! XViewController
var xIndex = vc.xPageIndex as Int
if ( (xIndex == 0) || (xIndex == NSNotFound) ) {
return nil
}
xIndex--
return self.viewControllerAtIndex(xIndex)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let vc = viewController as! XViewController
var xIndex = vc.xPageIndex as Int
if (xIndex == NSNotFound) {
return nil
}
xIndex++
if (xIndex == self.videoId.count)
{
return nil
}
return self.viewControllerAtIndex(xIndex)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
我是新手,所以如果我不清楚或者我没有遵守本网站的“发布规则”,我很抱歉。 如果有人对我如何实现这一点有任何想法,那就太棒了。
非常感谢,
于连