Skip to content

Instantly share code, notes, and snippets.

@Asinox
Last active August 29, 2015 14:15
Show Gist options
  • Save Asinox/2e4db29e0c906588518f to your computer and use it in GitHub Desktop.
Save Asinox/2e4db29e0c906588518f to your computer and use it in GitHub Desktop.
Swift version of an Objective-C UIPageControl tutorial (https://www.youtube.com/watch?v=abRCY5St_EM)
//
// WelcomeViewController.swift
//
// Created by Juan Miguel Calcano on 2/2/15.
// Copyright (c) 2015 Juan Miguel Calcano. All rights reserved.
// Swift version of an Objective-C UIPageControl tutorial (https://www.youtube.com/watch?v=abRCY5St_EM)
// UPDATE: swipe UIPageControl
import UIKit
class WelcomeViewController: UIViewController, UIScrollViewDelegate, UIGestureRecognizerDelegate {
@IBOutlet weak var callOutLabel: UILabel!
@IBOutlet weak var callOutPageControl: UIPageControl? = UIPageControl()
var callOutMsg:[String] = []
var currentMsg:String = String()
//swipe
var indexPage = 3
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.callOutMsg = ["Discover what's trendy within your friend", "Find things trendy near to you", "Share with style"]
self.currentMsg = callOutMsg[0]
self.callOutLabel?.text = currentMsg
//swipe
//PageControl Swipe
var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
swipeRight.numberOfTouchesRequired = 1
self.view.addGestureRecognizer(swipeRight)
var swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
swipeLeft.numberOfTouchesRequired = 1
self.view.addGestureRecognizer(swipeLeft)
}
@IBAction func callOutPageControlChanged(sender: AnyObject) {
let selectedPage = self.callOutPageControl?.currentPage
self.callOutLabel?.text = callOutMsg[selectedPage!]
}
//swipe
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
var totalPage = self.callOutMsg.count
var selectedPage = self.callOutPageControl?.currentPage
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
indexPage++
case UISwipeGestureRecognizerDirection.Left:
indexPage--
default:
break
}
indexPage = (indexPage < 0) ? (totalPage - 1):
indexPage % totalPage
print(indexPage)
self.callOutLabel?.text = callOutMsg[indexPage]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment