How to Build an UIScrollView in Swift 2.0 in iOS

Inwizards-blog-img

Now a days Swift is a creative platform to develop any application in iOS. Today we will see how to build an endless UIScrollView in Swift 2.0 in iOS. 

Let’s Start :

Phase One: We will try to make :

 

We will start off by beginning Xcode and developing a new undertaking, utilising the Solitary View Software theme.

Subsequently, open ViewController.swift and alter its school to UIPageViewController. Likewise inside the Storyboard, remove the ViewController you substitute it with a UIPageViewController item, and discover there. Ensure this new subject is is the Initial View Operator and that the category is placed for your ViewController.

You will need to go in and make certain the Site Controlis “Navigation” is Outside, that the “Transition Fashion” is ready to Search and that the “Spine Place” is placed to None.

Create a fresh Quick file in Xcode, and call it Indexable and within it, paste the next:


// 1 import UIKit extension UIViewController {   private struct AssociatedKeys {     static var id : Int?   }   var id: Int {     get {          return objc_getAssociatedObject(self, &AssociatedKeys.id) as! Int     } set {       objc_setAssociatedObject(self, &AssociatedKeys.id, newValue as Int, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)     }   }     }

Using the following, inside your ViewController.swift report, substitute the articles after this:

class ViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {      // 1   override func viewDidLoad() {     super.viewDidLoad()     delegate = self     dataSource = self     setViewControllers([viewControllerAtIndex(0)], direction: .Forward, animated: false, completion: nil)   }   // 2   func viewControllerAtIndex(index: Int) -> UIViewController {     var vc: UIViewController?           switch index {         case 0:           vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("First")           vc?.id = index           vc?.view.backgroundColor = .cyanColor()            case 1:           vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Second")           vc?.id = index           vc?.view.backgroundColor = .redColor()         default: break       }         return vc!   }   // 3   func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {     let index = viewController.id           switch index {            case 0:           return viewControllerAtIndex(1)         case 1:           return viewControllerAtIndex(0)         default:           return viewControllerAtIndex(0)       }   }   // 4       func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {     let index = viewController.id     switch index {       case 0:         return viewControllerAtIndex(1)       case 1:         return viewControllerAtIndex(0)       default:         return viewControllerAtIndex(1)     }   } }

Breakpoint

1: We should make sure the delegate properties are setto the existing school. You may needless to say externalise these into different objects to preserve things simple although /records, we’ll just fit them in here.

2: This function returns a UIViewController object in line with the list you present to it. It then sets the directory you offer to become the UIViewController’s id house, which we included above. If you don’t want to use a method Associated Materials plus a single subclass of UIViewController could do properly, but again to maintain things simple, I am doing things in this manner, to be able to avoid making a lot of records.

3: The quick fills before screen of the UIPageViewController with whatever UIViewController should come before the one you are seeing.

4: The immediate is filled by viewControllerAfterViewController after display of the UIPageViewConroller with whatsoever must come after the recent one.

If you run the app now, you’ll notice that it works perfectly, but there’re no dots to indicate the current page.

Horizontal :

 

Vertical :

 

Phase Two: I Will allow you to get my pretty, along with your little UIPageControl also! –

One thing if you utilize two supplied UIPageViewControllerDelegate techniques to add a UIPageControl for the display, you’ll recognize, is the fact that has a filthy .blackColor() background on it.

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {     return count // You'll need to add this. }    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {     return currentIndex // You'll need to add and track this too. }

And then, you want to make use of this handy Delegate method from the UIPageViewController.

// 1 func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {     if completed {     switch previousViewControllers[0].id {       case 0: pageControl.currentPage = 1       case 1: pageControl.currentPage = 0       default: break     }   } }

If you like this great! Meaning you are completed and you may shut the bill!

How to Build an UIScrollView in Swift 2.0 in iOS

Post navigation


0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x