Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dasdom committed Jan 29, 2016
0 parents commit b6eea3e
Show file tree
Hide file tree
Showing 15 changed files with 298 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//: [Previous](@previous)

import UIKit
import XCPlayground

class ViewController: UIViewController {

var allButtons: [UIButton]?

override func viewDidLoad() {
view.backgroundColor = .whiteColor()
view.tintColor = UIColor.whiteColor()

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Show/Hide", style: .Plain, target: self, action: "animate")
}

func animate() {
if let allButtons = allButtons {
hideButtons(allButtons)
} else {
showButtons()
}
}

func showButtons() {
let makeButtonWithTitle = { (title: String) -> UIButton in
let button = UIButton(type: .System)
button.backgroundColor = .brownColor()
button.setTitle(title, forState: .Normal)
button.layer.cornerRadius = 5
return button
}

let aButton = makeButtonWithTitle("Option a")
let bButton = makeButtonWithTitle("Option b")
let cButton = makeButtonWithTitle("Option c")

allButtons = [aButton, bButton, cButton]

guard let allButtons = allButtons else { fatalError() }

for button in allButtons.reverse() {
button.frame = CGRect(x: 10, y: -40, width: view.frame.size.width-20, height: 40)
view.addSubview(button)
}

for (index, button) in allButtons.enumerate() {

UIView.animateWithDuration(0.5, delay: 0.05 * Double(index), usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: [], animations: {

button.frame.origin.y = 60 + CGFloat(index) * (button.frame.size.height + 10)
}) { (_) in

}
}
}

func hideButtons(buttons: [UIButton]) {
for button in buttons {
UIView.animateWithDuration(0.5, animations: {
button.frame.origin.y = -40
button.alpha = 0
}, completion: { (_) in
button.removeFromSuperview()
})
}
self.allButtons = nil
}
}

let navigationController = UINavigationController(rootViewController: ViewController())

navigationController.view.frame.size = CGSize(width: 320, height: 400)
navigationController.navigationBar.barTintColor = .whiteColor()

XCPlaygroundPage.currentPage.liveView = navigationController.view

//: [Next](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//: [Previous](@previous)

import UIKit
import XCPlayground

class View: UIView {

let label: UILabel

override init(frame: CGRect) {

label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFontOfSize(20)
label.text = "Do something"
label.numberOfLines = 0
label.textAlignment = .Center

super.init(frame: frame)

addGestureRecognizer(UITapGestureRecognizer(target: self, action: "tap:"))

let leftSwipeRecogniser = UISwipeGestureRecognizer(target: self, action: "swipe:")
leftSwipeRecogniser.direction = UISwipeGestureRecognizerDirection.Left
addGestureRecognizer(leftSwipeRecogniser)

let rightSwipeRecogniser = UISwipeGestureRecognizer(target: self, action: "swipe:")
rightSwipeRecogniser.direction = UISwipeGestureRecognizerDirection.Right
addGestureRecognizer(rightSwipeRecogniser)

let panRecognizer = UIPanGestureRecognizer(target: self, action: "pan:")
panRecognizer.requireGestureRecognizerToFail(leftSwipeRecogniser)
panRecognizer.requireGestureRecognizerToFail(rightSwipeRecogniser)
addGestureRecognizer(panRecognizer)

backgroundColor = UIColor.whiteColor()

addSubview(label)

label.leadingAnchor.constraintEqualToAnchor(leadingAnchor).active = true
label.trailingAnchor.constraintEqualToAnchor(trailingAnchor).active = true
label.centerYAnchor.constraintEqualToAnchor(centerYAnchor).active = true
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func tap(sender: UITapGestureRecognizer) {
label.text = "tap"
}

func swipe(sender: UISwipeGestureRecognizer) {
let directionString: String
switch sender.direction {
case UISwipeGestureRecognizerDirection.Left:
directionString = "left"
case UISwipeGestureRecognizerDirection.Up:
directionString = "up"
case UISwipeGestureRecognizerDirection.Right:
directionString = "right"
case UISwipeGestureRecognizerDirection.Down:
directionString = "down"
default:
directionString = "w00t?"
}

label.text = "swipe \(directionString)"
}

func pan(sender: UIPanGestureRecognizer) {
label.text = "pan with velocity:\nx: \(sender.velocityInView(self).x)\ny: \(sender.velocityInView(self).y)"
}

}

let view = View(frame: CGRect(x: 0, y: 0, width: 300, height: 300))

XCPlaygroundPage.currentPage.liveView = view

//: [Next](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//: Playground - noun: a place where people can play

import UIKit
import XCPlayground

class TableViewController: UITableViewController {

override func viewDidLoad() {
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

cell.textLabel?.text = "Row: \(indexPath.row)"

return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let next = TableViewController()
next.title = "Row: \(indexPath.row)"
navigationController?.pushViewController(next, animated: true)

print(navigationController?.viewControllers.count)
}
}

let tableViewController = TableViewController()
tableViewController.title = "start"
let navigationController = UINavigationController(rootViewController: tableViewController)

navigationController.view.frame.size = CGSize(width: 320, height: 400)
navigationController.navigationBar.barTintColor = .whiteColor()

XCPlaygroundPage.currentPage.liveView = navigationController.view

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//: [Previous](@previous)

import UIKit
import XCPlayground

class View: UIView {

let textField: UITextField
let label: UILabel

override init(frame: CGRect) {

textField = UITextField()
textField.backgroundColor = UIColor.yellowColor()
textField.placeholder = "Type something"

label = UILabel()
label.backgroundColor = UIColor.blackColor()
label.textColor = UIColor.whiteColor()
label.text = " "

let button = UIButton(type: .System)
button.setTitle("I'm a button!", forState: .Normal)
button.tintColor = UIColor.whiteColor()

let stackView = UIStackView(arrangedSubviews: [textField, label, button])
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .Vertical
stackView.distribution = .EqualSpacing
stackView.spacing = 10

super.init(frame: frame)

backgroundColor = UIColor.brownColor()

addSubview(stackView)
button.addTarget(self, action: "changeLabelText", forControlEvents: .TouchUpInside)

let views = ["stackView": stackView]
var layoutConstraints = [NSLayoutConstraint]()
layoutConstraints += NSLayoutConstraint.constraintsWithVisualFormat("|-[stackView]-|", options: [], metrics: nil, views: views)
layoutConstraints += NSLayoutConstraint.constraintsWithVisualFormat("V:|-[stackView]", options: [], metrics: nil, views: views)
NSLayoutConstraint.activateConstraints(layoutConstraints)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func changeLabelText() {
label.text = textField.text
}
}

let hostView = View(frame: CGRect(x: 0, y: 0, width: 320, height: 200))

XCPlaygroundPage.currentPage.liveView = hostView

//: [Next](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
8 changes: 8 additions & 0 deletions InteractivePlayground.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='6.0' target-platform='ios'>
<pages>
<page name='Push View Controller'/>
<page name='Animated Buttons'/>
<page name='Gesture Recognizer'/>
</pages>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file added gifs/animated_buttons.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/gesture_recognizer.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/push_view_controller.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/text_input.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b6eea3e

Please sign in to comment.