我试图创建一个幻灯片ViewController,如snapchats

时间:2015-10-05 05:24:44

标签: ios swift uiviewcontroller

我从YouTube类似菜单视频复制了滑动视图控制器的代码,但无法编译。

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        var V1 :  View1 = View1(nibName: "View1" , bundle: nil)

        self.addChildViewController(V1)
        self.scrollView.addSubview(V1.view)
        V1.didMoveToParentViewController(self)


    }

我一直在使用未声明类型'View1'的错误。我该如何解决?我按照教程,代码是相同的。如何将View1声明为类型?

1 个答案:

答案 0 :(得分:1)

我已经制作了相同的教程,但有三个观点:

  @IBOutlet weak var scrollView: UIScrollView!

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let view1: View1 = View1(nibName: "View1", bundle: nil)

    addChildViewController(view1)
    scrollView.addSubview(view1.view)
    view1.didMoveToParentViewController(self)

    let view2: View2 = View2(nibName: "View2", bundle: nil)

    addChildViewController(view2)
    scrollView.addSubview(view2.view)
    view2.didMoveToParentViewController(self)

    var view2Frame: CGRect = view2.view.frame
    view2Frame.origin.x = view.frame.width
    view2.view.frame = view2Frame


    let view3: View3 = View3(nibName: "View3", bundle: nil)

    addChildViewController(view3)
    scrollView.addSubview(view3.view)
    view3.didMoveToParentViewController(self)

    var view3Frame: CGRect = view3.view.frame
    view3Frame.origin.x = view.frame.width * 2
    view3.view.frame = view3Frame

    self.scrollView.contentSize.width = view.frame.width * 3
  }

这很好用。希望它有所帮助。