如何在swift中创建多个mp4或mov文件的数组。我能够在uiwebview中显示单个视频。我有页面控件来显示一些文字,但我需要显示与文本相同的不同视频。当页面控件启动时,它应显示下一个视频。这是第一个视图控制器和页面视图控制器的代码。
import UIKit
class ViewController: UIViewController,UIPageViewControllerDataSource {
//var pageImages:NSArray!
var ouotes: NSArray!
var video: NSArray!
var pageViewController:UIPageViewController!
@IBOutlet weak var GenerateNumbers: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
video = ["2.mov", "1.mov", "4.mov"]
ouotes = ["sometext" ]
// NSArray(objects:"ap", "bg", "gfb")
/* UIGraphicsBeginImageContext(self.view.frame.size)
UIImage(named: "money")?.draw(in: self.view.bounds)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
self.view.backgroundColor = UIColor(patternImage: image)*/
self.pageViewController = self.storyboard?.instantiateViewController(withIdentifier: "MyPageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
let initialContenViewController = self.pageTutorialAtIndex(0) as ContentHolder
self.pageViewController.setViewControllers([initialContenViewController], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: self.view.frame.size.height-100)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMove(toParentViewController: self)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pageTutorialAtIndex(_ index: Int) -> ContentHolder {
let pageContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "ContentHolder") as! ContentHolder
pageContentViewController.imageFileName = ouotes[index] as! String
pageContentViewController.videoFileName = video [index] as! String
pageContentViewController.pageIndex = index
return pageContentViewController
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
{
let viewController = viewController as! ContentHolder
var index = viewController.pageIndex as Int
if (index == 0 || index == NSNotFound) {
return nil
}
index -= 1
return self.pageTutorialAtIndex(index)
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
{
let viewController = viewController as! ContentHolder
var index = viewController.pageIndex as Int
if ((index == NSNotFound)) {
return nil
}
index += 1
if (index == ouotes.count) {
return nil
}
if (index == video.count) {
return nil
}
return self.pageTutorialAtIndex(index)
}
public func presentationCount(for pageViewController: UIPageViewController) -> Int {
return ouotes.count
}
public func presentationIndex(for pageViewController: UIPageViewController) -> Int{
return 0
}
}
对于应显示文本和视频的页面控制视图
import Foundation
import UIKit
import AVKit
import AVFoundation
class ContentHolder: UIViewController {
var imageFileName: String!
var videoFileName: String!
var pageIndex:Int!
@IBOutlet weak var EuroScrollView: UIScrollView!
@IBOutlet weak var VideoView: UIWebView!
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// let fileURL = NSURL (fileURLWithPath: "/Users/nafu/Desktop/2.mov")
VideoView.loadHTMLString("<iframe width = \"\(self.VideoView.frame.width) \" height =\"\(self.VideoView.frame.height)\" src = \"\(videoFileName)\"> </iframe>", baseURL: nil)
myLabel.text = imageFileName
myLabel.numberOfLines = 0
myLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
myLabel.font = UIFont(name:"HelveticaNeue-Bold", size: 15.0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
提前致谢。