IOS UIPageViewController - 只滑动屏幕的一部分
IOS UIPageViewController - Swipe only some part of the screen
我正在尝试设计一个包含滑动部分的屏幕。我有一个屏幕和一个图像视图,底部有两个按钮。我只想滑动图像视图而不是底部区域。画面是这样
目前的问题是滑动整个屏幕,包括按钮。
这是我的代码
ViewController.swift
import UIKit
class ViewController: UIViewController, UIPageViewControllerDataSource {
var pageImages:NSArray!
var pageViewController:UIPageViewController!
override func viewDidLoad() {
super.viewDidLoad()
pageImages = NSArray(objects: "startscreen1","startscreen2","startscreen3")
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("MyPageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
let initialContenViewController = self.pageTutorialAtIndex(0) as TutorialScreenViewController
let viewControllers = NSArray(object: initialContenViewController)
self.pageViewController.setViewControllers(viewControllers as! [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
/* self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)*/
self.pageViewController.view.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height-100)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pageTutorialAtIndex(index: Int) -> TutorialScreenViewController
{
let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("TutorialScreenViewController") as! TutorialScreenViewController
pageContentViewController.imageFileName = pageImages[index] as! String
pageContentViewController.pageIndex = index
return pageContentViewController
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
let viewController = viewController as! TutorialScreenViewController
var index = viewController.pageIndex as Int
if(index == 0 || index == NSNotFound)
{
return nil
}
index--
return self.pageTutorialAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
{
let viewController = viewController as! TutorialScreenViewController
var index = viewController.pageIndex as Int
if((index == NSNotFound))
{
return nil
}
index++
if(index == pageImages.count)
{
return nil
}
return self.pageTutorialAtIndex(index)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return pageImages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int // The selected item reflected in the page indicator.
{
return 0
}
}
教程屏幕ViewController.swift
@IBOutlet weak var myImageView: UIImageView!
var imageFileName: String!
var pageIndex:Int!
var width:CGFloat!
@IBOutlet weak var loginButton: UIButton!
@IBOutlet weak var registerButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
myImageView.image = UIImage(named:imageFileName)
}
您可以使用视图控制器包含:
创建一个具有两个按钮和 "container" 视图的标准视图控制器。只需在界面生成器右侧面板的对象库中搜索 "container",然后将其拖到主视图控制器的场景中:
显然,设置您的约束,以便使用按钮正确布置此容器视图。
现在控制-从容器视图拖到页面视图控制器,然后选择"embed" segue。
你最终会得到一个看起来像这样的场景:
现在您可以在带有其他按钮的标准视图控制器中享受页面视图控制器的功能。
我正在尝试设计一个包含滑动部分的屏幕。我有一个屏幕和一个图像视图,底部有两个按钮。我只想滑动图像视图而不是底部区域。画面是这样
目前的问题是滑动整个屏幕,包括按钮。
这是我的代码
ViewController.swift
import UIKit
class ViewController: UIViewController, UIPageViewControllerDataSource {
var pageImages:NSArray!
var pageViewController:UIPageViewController!
override func viewDidLoad() {
super.viewDidLoad()
pageImages = NSArray(objects: "startscreen1","startscreen2","startscreen3")
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("MyPageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
let initialContenViewController = self.pageTutorialAtIndex(0) as TutorialScreenViewController
let viewControllers = NSArray(object: initialContenViewController)
self.pageViewController.setViewControllers(viewControllers as! [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
/* self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)*/
self.pageViewController.view.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height-100)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pageTutorialAtIndex(index: Int) -> TutorialScreenViewController
{
let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("TutorialScreenViewController") as! TutorialScreenViewController
pageContentViewController.imageFileName = pageImages[index] as! String
pageContentViewController.pageIndex = index
return pageContentViewController
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
let viewController = viewController as! TutorialScreenViewController
var index = viewController.pageIndex as Int
if(index == 0 || index == NSNotFound)
{
return nil
}
index--
return self.pageTutorialAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
{
let viewController = viewController as! TutorialScreenViewController
var index = viewController.pageIndex as Int
if((index == NSNotFound))
{
return nil
}
index++
if(index == pageImages.count)
{
return nil
}
return self.pageTutorialAtIndex(index)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return pageImages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int // The selected item reflected in the page indicator.
{
return 0
}
}
教程屏幕ViewController.swift
@IBOutlet weak var myImageView: UIImageView!
var imageFileName: String!
var pageIndex:Int!
var width:CGFloat!
@IBOutlet weak var loginButton: UIButton!
@IBOutlet weak var registerButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
myImageView.image = UIImage(named:imageFileName)
}
您可以使用视图控制器包含:
创建一个具有两个按钮和 "container" 视图的标准视图控制器。只需在界面生成器右侧面板的对象库中搜索 "container",然后将其拖到主视图控制器的场景中:
显然,设置您的约束,以便使用按钮正确布置此容器视图。
现在控制-从容器视图拖到页面视图控制器,然后选择"embed" segue。
你最终会得到一个看起来像这样的场景:
现在您可以在带有其他按钮的标准视图控制器中享受页面视图控制器的功能。