ScrollView paging

时间:2015-06-30 13:50:39

标签: ios swift uiscrollview xcode6

I want to create the slide to unlock animation like it is there on the iPhone lock screen. I want the user to swipe right so that another view comes to the front. How should I make this? I have tried this, and here is my code:-

import UIKit

class PageViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    setUpScrollView()
}

func setUpScrollView () {

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

    let aViewController = storyboard.instantiateViewControllerWithIdentifier("Hello") as! HelloViewController;
    let bViewController = storyboard.instantiateViewControllerWithIdentifier("Home") as! HomeViewController;

    let viewControllers = [bViewController, aViewController]

    scrollView.pagingEnabled = true
    scrollView.contentSize.height = 600

    var contentSizeWidth = CGFloat(2) * self.view.frame.width
    scrollView.contentSize.width =  contentSizeWidth
    scrollView.showsHorizontalScrollIndicator = false
    scrollView.showsVerticalScrollIndicator = false
    scrollView.scrollsToTop = false
    scrollView.delegate = self

    // add all views to scrollView
    for (index, vc) in enumerate(viewControllers) {
        var frame = self.view.frame
        frame.origin.x = frame.width * CGFloat(index)
        frame.origin.y = 0
        vc.view.frame = frame

        self.addChildViewController(vc)
        self.scrollView.addSubview(vc.view)
        vc.didMoveToParentViewController(self)
    }
       }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

The problem that I face is, that the view on the left is cropped somehow. To make you visualise this, the view with the slide to unlock label appears when the iOS simulator boots up. But when I swipe right, the scrollView works perfectly, just the second view is cropped (it is on the extreme left, occupying less than half of the screen that it should ideally fully occupy. Please help me out here. Thanks in advance.

0 个答案:

没有答案