Skip to content

Instantly share code, notes, and snippets.

@avnerbarr
Created May 2, 2015 19:53
Show Gist options
  • Save avnerbarr/a46b1f651b16f647389e to your computer and use it in GitHub Desktop.
Save avnerbarr/a46b1f651b16f647389e to your computer and use it in GitHub Desktop.
Test parallax controller
//
// ViewController.swift
// ParallaxTableVIew
//
// Created by Avner on 5/2/15.
// Copyright (c) 2015 Avner. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDataSource, ParallaxControllerDelegate {
let tableView = UITableView()
let kCellIdentifier = "cell"
var parallaxView = UIView()
let parallaxHeight : CGFloat = 200
let statusBarHeight : CGFloat = 20
var label : UILabel!
var parallaxController : ParallaxController?
var navigationBarHeight : CGFloat {
get {
if let navBarHeight = self.navigationController?.navigationBar.frame.height {
return navBarHeight + statusBarHeight
}
return 0
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(tableView)
tableView.frame = CGRectMake(0, navigationBarHeight + parallaxHeight, self.view.bounds.width, self.view.bounds.height - navigationBarHeight)
tableView.dataSource = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: kCellIdentifier)
createParallaxView()
self.view.addSubview(parallaxView)
parallaxController = ParallaxController(parentViewController: self, tableView: self.tableView, parallaxView: parallaxView, parallaxHeight: parallaxHeight, navigationBarHeight: navigationBarHeight)
parallaxController?.delegate = self
}
func parallaxController(controller : ParallaxController,intersectNavBarPercent : CGFloat) {
label.alpha = intersectNavBarPercent
}
//MARK: UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier, forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = indexPath.description
return cell
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 10
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)"
}
func createParallaxView() {
parallaxView = UIView(frame: CGRectMake(0, 0, self.view.frame.width, parallaxHeight+navigationBarHeight))
parallaxView.backgroundColor = UIColor.blueColor()
label = UILabel(frame: CGRectMake(0, parallaxView.frame.height-40, parallaxView.frame.width, 35))
parallaxView.addSubview(label)
label.font = UIFont.systemFontOfSize(30)
label.textColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
label.text = "Navigation Bar Title"
label.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin
label.alpha = 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment