IOS UIPageViewController - 仅滑动屏幕的某些部分

时间:2015-12-07 16:41:40

标签: ios iphone swift uipageviewcontroller swipe-gesture

我试图设计一个包含滑动部分的屏幕。我有一个带有一个imageview的屏幕,底部有两个按钮。我想只滑动imageview而不是底部区域。屏幕就像这样

enter image description here

目前问题是整个屏幕都在滑动,包括按钮。

这是我的代码

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

    }


}

TutorialScreenViewController.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)

            }

1 个答案:

答案 0 :(得分:10)

您可以使用视图控制器包含:

  1. 创建一个标准视图控制器,它具有两个按钮和“容器”视图。只需在“界面”构建器的右侧面板上的对象库中搜索“容器”,然后将其拖到主视图控制器的场景中:

    enter image description here

    显然,设置约束以便使用按钮正确布置此容器视图。

  2. 现在 control -drag从容器视图到你的页面视图控制器,然后选择“embed”segue。

  3. 你最终会得到一个看起来像的场景:

    enter image description here

    现在,您可以在标准视图控制器中使用其他按钮欣赏页面视图控制器功能。