Last active
August 29, 2015 14:07
-
-
Save andreif/eb76af0904937d16f699 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
self.window = UIWindow(frame: UIScreen.mainScreen().bounds) | |
self.window!.makeKeyAndVisible() | |
let controller = TapController(nibName: nil, bundle: nil) | |
let nav_controller = UINavigationController(rootViewController: controller) | |
let tab_controller = UITabBarController(nibName: nil, bundle: nil) | |
let other_controller = UIViewController(nibName: nil, bundle: nil) | |
other_controller.title = "Other" | |
other_controller.view.backgroundColor = UIColor.purpleColor() | |
tab_controller.viewControllers = [nav_controller!, other_controller] | |
self.window!.rootViewController = tab_controller | |
return true | |
} | |
} | |
class TapController: UIViewController { | |
var label: UILabel? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.backgroundColor = UIColor.whiteColor() | |
self.label = UILabel(frame: CGRectZero) | |
self.label!.text = "Taps" | |
self.label!.sizeToFit() | |
self.label!.center = CGPoint( | |
x: self.view.frame.size.width / 2, | |
y: self.view.frame.size.height / 2) | |
self.view.addSubview(self.label!) | |
self.title = "Tap \(self.navigationController!.viewControllers.count)" | |
let right_button = UIBarButtonItem(title: "Push", style: UIBarButtonItemStyle.Bordered, target: self, action: "push") | |
self.navigationItem.rightBarButtonItem = right_button | |
} | |
func push() { | |
let new_controller = TapController(nibName: nil, bundle: nil) | |
self.navigationController!.pushViewController(new_controller, animated: true) | |
} | |
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { | |
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) | |
self.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Favorites, tag: 1) | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
self.window = UIWindow(frame: UIScreen.mainScreen().bounds) | |
self.window!.makeKeyAndVisible() | |
self.window!.rootViewController = AlphabetController(nibName: nil, bundle: nil) | |
return true | |
} | |
} | |
class AlphabetController: UIViewController, UITableViewDelegate, UITableViewDataSource { | |
var tableView: UITableView? | |
var data: Array<String>? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.title = "Alphabet" | |
self.tableView = UITableView(frame: self.view.bounds) | |
self.view.addSubview(self.tableView!) | |
self.tableView!.dataSource = self | |
self.tableView!.delegate = self | |
self.data = map(UnicodeScalar("A").value...UnicodeScalar("Z").value, {(val: UInt32) -> String in return "\(UnicodeScalar(val))";}) | |
//self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "CELL") FAILS FOR detailTextLabel | |
self.tableView!.registerClass(MyCell.self, forCellReuseIdentifier: "CELL") | |
} | |
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return self.data!.count | |
} | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
// FAILS FOR detailTextLabel: | |
//var cell = tableView.dequeueReusableCellWithIdentifier("CELL", forIndexPath:indexPath) as? UITableViewCell | |
//if cell == nil { cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "CELL") } | |
var cell = tableView.dequeueReusableCellWithIdentifier("CELL", forIndexPath:indexPath) as? MyCell | |
if cell == nil { cell = MyCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "CELL") } | |
//we know that cell is not empty now so we use ! to force unwrapping | |
cell!.textLabel!.text = self.data![indexPath.row] | |
cell!.detailTextLabel!.text = "1/2 cup" | |
//cell.cellImage.image = UIImage(named: "0.jpg") | |
return cell! | |
} | |
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { | |
tableView.deselectRowAtIndexPath(indexPath, animated: true) | |
let alert = UIAlertView() | |
alert.message = "\(self.data![indexPath.row]) tapped!" | |
alert.addButtonWithTitle("OK") | |
alert.show() | |
} | |
} | |
class MyCell: UITableViewCell { | |
override init?(style: UITableViewCellStyle, reuseIdentifier: String?) { | |
super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier) | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { | |
let pageControl = UIPageControl.appearance() | |
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor() | |
pageControl.currentPageIndicatorTintColor = UIColor.blackColor() | |
pageControl.backgroundColor = UIColor.whiteColor() | |
self.window = UIWindow(frame: UIScreen.mainScreen().bounds) | |
self.window!.makeKeyAndVisible() | |
//let view = UIImageView(frame: self.window!.frame) | |
//view.image = UIImage(named: "page1") | |
//self.window!.addSubview(view) | |
self.window!.rootViewController = ViewController(nibName: nil, bundle: nil) | |
return true | |
} | |
} | |
class ViewController: UIViewController, UIPageViewControllerDataSource { | |
var pageViewController : UIPageViewController? | |
var pageTitles : Array<String> = ["God vs Man", "Cool Breeze", "Fire Sky"] | |
var pageImages : Array<String> = ["page1", "page2", "page3"] | |
var currentIndex : Int = 0 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil) | |
self.pageViewController!.dataSource = self | |
let startingViewController: PageContentViewController = self.viewControllerAtIndex(0)! | |
let viewControllers: NSArray = [startingViewController] | |
self.pageViewController!.setViewControllers(viewControllers, direction: .Forward, animated: false, completion: nil) | |
self.pageViewController!.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30); | |
self.addChildViewController(self.pageViewController!) | |
self.view.addSubview(self.pageViewController!.view) | |
self.pageViewController!.didMoveToParentViewController(self) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? { | |
var index = (viewController as PageContentViewController).pageIndex | |
if (index == 0) || (index == NSNotFound) { | |
return nil | |
} | |
index-- | |
println("Decreasing Index: \(String(index))") | |
return self.viewControllerAtIndex(index) | |
} | |
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? { | |
var index = (viewController as PageContentViewController).pageIndex | |
if index == NSNotFound { | |
return nil | |
} | |
index++ | |
println("Increasing Index: \(String(index))") | |
if (index == self.pageTitles.count) { | |
return nil | |
} | |
return self.viewControllerAtIndex(index) | |
} | |
func viewControllerAtIndex(index: Int) -> PageContentViewController? { | |
if self.pageTitles.count == 0 || index >= self.pageTitles.count { | |
return nil | |
} | |
self.currentIndex = index | |
// Create a new view controller and pass suitable data. | |
let pageContentViewController = PageContentViewController(nibName: nil, bundle: nil) | |
pageContentViewController.setup(index, image: self.pageImages[index], title: self.pageTitles[index]) | |
return pageContentViewController | |
} | |
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int { | |
return self.pageTitles.count | |
} | |
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int { | |
return 0 | |
} | |
} | |
class PageContentViewController: UIViewController { | |
var backgroundImageView : UIImageView? | |
var titleLabel : UILabel? | |
var pageIndex : Int = 0 | |
var titleText : String = "" | |
var imageFile : String = "" | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
func not_set() -> Bool { | |
return (self.backgroundImageView == nil) | |
} | |
func setup(index: Int, image: String, title: String) { | |
if self.not_set() { | |
self.backgroundImageView = UIImageView(frame: self.view.frame) | |
self.titleLabel = UILabel(frame: self.view.frame) | |
self.view.addSubview(self.backgroundImageView!) | |
self.view.addSubview(self.titleLabel!) | |
} | |
self.pageIndex = index | |
self.imageFile = image | |
self.titleText = title | |
//let img = UIImage(named: image) | |
let img = UIImage(data: NSData(contentsOfURL: NSURL(string:"https://raw.githubusercontent.com/swiftcast/Swift-Example-UIPageViewController/master/Swift%20Pages/Images.xcassets/\(image).imageset/\(image).png")!)!) | |
self.backgroundImageView!.image = img | |
self.titleLabel!.text = title | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// TODO: Dispose of any resources that can be recreated. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment