Skip to content

Instantly share code, notes, and snippets.

@sashalondon
Created March 4, 2015 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sashalondon/13bf43cf161f1cf852aa to your computer and use it in GitHub Desktop.
Save sashalondon/13bf43cf161f1cf852aa to your computer and use it in GitHub Desktop.
[Swift 1] Day 6-7 - App Challenge - Word Magnets
//
// ViewController.swift
// Word Magnet game
//
// Created by Sasha Akhavan-Zanjani on 28/02/2015.
// Copyright (c) 2015 Sasha Akhavan-Zanjani. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var wordInput: UITextField!
// setup variables
var wordArray = [""]
var lastAddedLabel: UILabel!
var label: UILabel!
var firstLabel: UILabel!
var secondLabel: UILabel!
var fontsizefloat = 32.222
// set up RGB colour Pallete
func UIColorFromRGB(rgbValue: UInt) -> UIColor {
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
firstLabel = UILabel()
secondLabel = UILabel()
view.backgroundColor = UIColorFromRGB(0x3498db)
// Sets up font
let fontFamilyNames = UIFont.familyNames()
for familyName in fontFamilyNames {
println("Font Family Name = [\(familyName)]")
let names = UIFont.fontNamesForFamilyName(familyName as String)
println("Font Names = [\(names)]")
}
makeFirstLabel()
}
// Remove keyboard when you press return
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true;
}
// Set up touch gestures.
func handlePanGesture(panGesture: UIPanGestureRecognizer) {
println("pan")
// get translation
var translation = panGesture.translationInView(view)
panGesture.setTranslation(CGPointZero, inView: view)
println(translation)
// add dx, dy to current label center position
var label = panGesture.view as UILabel
label.center = CGPoint(x: label.center.x + translation.x,
y: label.center.y + translation.y)
}
// Remove word from array and remove from screen
func removeLabel(){
var label = UILabel()
let removed = wordArray.removeLast()
println(wordArray)
for v in view.subviews{
if v is UILabel{
lastAddedLabel.removeFromSuperview()
println(lastAddedLabel)
}
}
}
func removeAllLabels(){
var label = UILabel()
let removed = wordArray.removeAll()
println(wordArray)
for v in view.subviews{
if v is UILabel{
v.removeFromSuperview()
}
}
makeLabel()
}
func removeFirstLabel(){
var label = UILabel()
let removed = wordArray.removeLast()
println(wordArray)
for v in view.subviews{
if v is UILabel{
label.removeFromSuperview()
println(lastAddedLabel)
}
}
makeLabel()
}
// Takes new word adds it to page and then adds it to array
func addNewLabel(){
let yellowColour = UIColorFromRGB(0xFDFD96)
let blueColour = UIColorFromRGB(0xAEC6CF)
let pinkColour = UIColorFromRGB(0xDEA5A4)
let greyColour = UIColorFromRGB(0xCFCFC4)
let greenColour = UIColorFromRGB(0x77DD77)
let purpleColour = UIColorFromRGB(0x8e44ad)
var newWord = wordInput.text
wordArray.append(newWord)
println(wordArray)
var label = UILabel()
label.text = wordInput.text // wordArray[4]
label.font = UIFont(name: "montserrat", size: 28.0)
label.sizeToFit()
label.backgroundColor = UIColorFromRGB(0x8e44ad)
label.textColor = UIColorFromRGB(0xffffff)
label.alpha = 0.0 //is invisible
var x = CGFloat(arc4random_uniform(320))
var y = CGFloat(arc4random_uniform(480))
label.center = CGPoint(x: x, y: y)
lastAddedLabel = label
println(lastAddedLabel)
view.addSubview(label)
UIView.animateWithDuration(2.0, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1, options: nil, animations: {
self.lastAddedLabel.center = CGPoint(x: x , y: y + 120 )
self.lastAddedLabel.alpha = 1.0 //is visible
}, completion: nil)
// Pan Gesture
var panGesture = UIPanGestureRecognizer(target: self, action: Selector("handlePanGesture:"))
label.addGestureRecognizer(panGesture)
label.userInteractionEnabled = true
}
// Adds all words to screen from array
func makeLabel(){
for word in wordArray {
var label = UILabel()
label.text = word // wordArray[4]
label.font = UIFont(name: "orangejuice", size: 20.0)
println(lastAddedLabel)
label.sizeToFit()
label.center = CGPoint(x: 150, y: 200)
label.backgroundColor = UIColor.whiteColor()
var xa = CGFloat(arc4random_uniform(320))
var ya = CGFloat(arc4random_uniform(480))
label.center = CGPoint(x: xa, y: ya)
view.addSubview(label)
UIView.animateWithDuration(2.0, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.2, options: nil, animations: {
// self.label.center = CGPoint(x: xa , y: ya + 50 )
// self.label.alpha = 1.0 //is visible
}, completion: nil)
// Pan Gesture
var panGesture = UIPanGestureRecognizer(target: self, action: Selector("handlePanGesture:"))
label.addGestureRecognizer(panGesture)
label.userInteractionEnabled = true
}
}
func makeFirstLabel(){
firstLabel.text = "Make words"
firstLabel.font = UIFont(name: "montserrat", size: 34.0)
firstLabel.sizeToFit()
firstLabel.backgroundColor = UIColorFromRGB(0xfdfd96)
firstLabel.center = CGPoint(x: 150, y: 30)
firstLabel.textColor = UIColorFromRGB(0x8e44ad)
view.addSubview(firstLabel)
UIView.animateWithDuration(2.0, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.2, options: nil, animations: {
self.firstLabel.center = CGPoint(x:150, y:200)
}, completion: nil)
secondLabel.text = "and organise them"
secondLabel.font = UIFont(name: "montserrat", size: 34.0)
secondLabel.sizeToFit()
secondLabel.backgroundColor = UIColorFromRGB(0xccffff)
secondLabel.textColor = UIColorFromRGB(0xd35400)
secondLabel.center = CGPoint(x: 200, y: 60)
view.addSubview(secondLabel)
secondLabel.alpha = 0.0 //is invisible
UIView.animateWithDuration(2.0, delay: 0.8, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.2, options: nil, animations: {
self.secondLabel.center = CGPoint(x:200, y:260 )
self.secondLabel.alpha = 1.0 //is visible
}, completion: nil)
// Pan Gesture
var panGesture = UIPanGestureRecognizer(target: self, action: Selector("handlePanGesture:"))
var panGesture2 = UIPanGestureRecognizer(target: self, action: Selector("handlePanGesture:"))
firstLabel.addGestureRecognizer(panGesture)
firstLabel.userInteractionEnabled = true
secondLabel.addGestureRecognizer(panGesture2)
secondLabel.userInteractionEnabled = true
removeFirstLabel()
}
// Buttons and actions
@IBAction func addWordButton(sender: AnyObject) {
addNewLabel()
}
@IBAction func newWordTextField(sender: UITextField) {
var newWord = wordInput.text
}
@IBAction func removeWordButton(sender: AnyObject) {
let firstCheck = wordArray.isEmpty
var firstCount = wordArray.count
if firstCount != 0 {
removeLabel()
}
}
@IBAction func clearAllButton(sender: AnyObject) {
removeAllLabels()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// 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