UIScrollView& UIPageControl - 从SWIFT的特定页面开始

时间:2015-06-28 20:54:48

标签: ios swift uiviewcontroller uiscrollview uipagecontrol

我已经实现了UIScrollView& UIpageControl显示3个UIViewControllers。 它工作得很好,但我想在第二个UIViewController上启动显示。

我没有发现我的错在哪里:

var pageControl:UIPageControl!
var scrollView:UIScrollView!
var viewtest1:UIViewController!
var viewtest2:UIViewController!
var viewtest3:UIViewController!

override func viewDidLoad() {
    super.viewDidLoad()

    // Initialization of UIScrollView
    self.scrollView = UIScrollView()
    self.scrollView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
    self.scrollView.pagingEnabled = true
    self.scrollView.scrollEnabled = true
    self.scrollView.showsHorizontalScrollIndicator = false
    self.scrollView.showsVerticalScrollIndicator = false
    self.scrollView.delegate = self

    // Initialisation of UIPageControl
    self.pageControl = UIPageControl()

    pageControl.currentPageIndicatorTintColor = UIColor(red:0.325, green:0.667, blue:0.922, alpha: 1)
    pageControl.pageIndicatorTintColor = UIColor.whiteColor()
    pageControl.backgroundColor = UIColor.clearColor()

    // Add different view to the screen in the order from left to right

    self.addChildViewController(self.viewtest1)
    self.addChildViewController(self.viewtest2)
    self.addChildViewController(self.viewtest3)

}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)

    for var i=0; i < self.childViewControllers.count; i++ {
        self.loadScrollViewWithPage(i)

    }

    self.pageControl.currentPage = 0
    self.page = 0

    self.pageControl.numberOfPages = self.childViewControllers.count

    var viewController:UIViewController = self.childViewControllers[self.pageControl.currentPage] as! UIViewController

    if viewController.view.superview != nil {
        viewController.viewWillAppear(true)
    }

    self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * CGFloat(self.childViewControllers.count), scrollView.frame.size.height)

}


override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    // Load Controllers
    var viewController:UIViewController = self.childViewControllers[self.pageControl.currentPage] as! UIViewController

    if viewController.view.superview != nil {
        viewController.viewDidAppear(true)
    }
}

override func viewWillDisappear(animated: Bool) {
    var viewController:UIViewController = self.childViewControllers[self.pageControl.currentPage] as! UIViewController

    if viewController.view.superview != nil {
        viewController.viewWillDisappear(true)
    }

    super.viewWillDisappear(true)
}

override func viewDidDisappear(animated: Bool) {
    var viewController:UIViewController = self.childViewControllers[self.pageControl.currentPage] as! UIViewController

    if viewController.view.superview != nil {
        viewController.viewDidDisappear(true)
    }

    super.viewDidDisappear(true)
}

func loadScrollViewWithPage(page:Int){
    if page < 0 || page >= self.childViewControllers.count {
        return
    }

    let controller:UIViewController = self.childViewControllers[page] as! UIViewController

    if (controller == false) {
        return
    }

    // add the controller's view to the scroll view
    if (controller.view.superview == nil) {
        var frame:CGRect = self.scrollView.frame
        frame.origin.x = frame.size.width * CGFloat(page)
        frame.origin.y = 0
        controller.view.frame = frame
        self.scrollView.addSubview(controller.view)
    }

}

func changePage(sender:AnyObject) -> () {
    var senderControl:UIPageControl = sender as! UIPageControl
    var page:Int = Int(senderControl.currentPage) 

    // update the scroll view to the appropriate page
    var frame:CGRect = self.scrollView.frame
    frame.origin.x = frame.size.width * CGFloat(page)
    frame.origin.y = 0

    var oldViewController:UIViewController = self.childViewControllers[self.page] as! UIViewController

    var newViewController:UIViewController = self.childViewControllers[self.pageControl.currentPage] as! UIViewController


    oldViewController.viewWillDisappear(true)
    newViewController.viewWillAppear(true)

    self.scrollView.scrollRectToVisible(frame, animated: true)

    self.pageControlUsed = true

}

override func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {

    var oldViewController:UIViewController = self.childViewControllers[self.page] as! UIViewController
    var newViewController:UIViewController = self.childViewControllers[self.pageControl.currentPage] as! UIViewController

    oldViewController.viewWillDisappear(true)
    newViewController.viewWillAppear(true)

    self.page = self.pageControl.currentPage

}

override func scrollViewDidScroll(scrollView: UIScrollView) {
    if (self.pageControlUsed == true /*|| _rotating*/) {
        // do nothing - the scroll was initiated from the page control, not the user dragging
        return
    }

    var pageWidth:CGFloat = self.scrollView.frame.size.width
    var page:Int = Int(floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1)
    if (self.pageControl.currentPage != page) {

        var oldViewController:UIViewController = self.childViewControllers[self.pageControl.currentPage] as! UIViewController

        var newViewController:UIViewController = self.childViewControllers[page] as! UIViewController


        oldViewController.viewWillDisappear(true)
        newViewController.viewWillDisappear(true)
        self.pageControl.currentPage = page
        oldViewController.viewDidDisappear(true)
        newViewController.viewDidDisappear(true)

        self.page = page;
    }
}

override func scrollViewWillBeginDragging(scrollView: UIScrollView) {
    self.pageControlUsed = false
}

override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    self.pageControlUsed = false

}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

在故事板中设置&#34; viewtest2&#34;到&#34;是初始视图控制器&#34;。那将首先显示viewController。

或者您可以在appDelegate中尝试:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject :AnyObject]?) -> Bool {

 self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    var storyboard = UIStoryboard(name: "Main", bundle: nil)

    var initialViewController = storyboard.instantiateViewControllerWithIdentifier("viewTest2") as UIViewController

    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
}