Skip to content

Instantly share code, notes, and snippets.

@eloisecamire
Last active August 29, 2015 14:07
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 eloisecamire/b585c76498d23f5a257c to your computer and use it in GitHub Desktop.
Save eloisecamire/b585c76498d23f5a257c to your computer and use it in GitHub Desktop.
Word Magnets - Day 6 and 7 Challenge
//
// ViewController.swift
// Word Magnets
//
// Created by Eloise Camire on 2014-10-11.
// Copyright (c) 2014 Eloise Camire. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var wordInput: UITextField!
@IBOutlet weak var bkgImage: UIImageView!
var wordArray = ["I", "like", "to", "program", "app", "my", "favorite", "food", "pizza", "Eloise", "friends", "am", "are", "danger", "Mr. White", "Eisenberg"]
//function to add RGB Color
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.
createLabels()
//view.backgroundColor = UIColor.yellowColor()
view.backgroundColor = UIColorFromRGB(0xCCFF99)
println("Size in points: \(view.bounds.size)")
var bounds: CGRect = UIScreen.mainScreen().bounds
var width:CGFloat = bounds.size.width
var height:CGFloat = bounds.size.height
bkgImage.sizeToFit()
}
func handlePanGesture(panGesture: UIPanGestureRecognizer){
println("pan")
// get the 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)
// add velocity when pan gesture end - code from Caroline Begbie tutorial at raywenderlich.com
if panGesture.state == UIGestureRecognizerState.Ended {
// 1
let velocity = panGesture.velocityInView(self.view)
let magnitude = sqrt((velocity.x * velocity.x) + (velocity.y * velocity.y))
let slideMultiplier = magnitude / 200
println("magnitude: \(magnitude), slideMultiplier: \(slideMultiplier)")
// 2
let slideFactor = 0.05 * slideMultiplier //Increase for more of a slide
// 3
var finalPoint = CGPoint(x:panGesture.view!.center.x + (velocity.x * slideFactor),
y:panGesture.view!.center.y + (velocity.y * slideFactor))
// 4
finalPoint.x = min(max(finalPoint.x, 0), self.view.bounds.size.width)
finalPoint.y = min(max(finalPoint.y, 0), self.view.bounds.size.height)
// 5
UIView.animateWithDuration(Double(slideFactor * 1.5),
delay: 0,
// 6
options: UIViewAnimationOptions.CurveEaseOut,
animations: {panGesture.view!.center = finalPoint },
completion: nil)
}
}
// create a new label and add new item to original array
func addNewLabel(){
var newWord = wordInput.text
wordArray.append(newWord)
println(wordArray)
var label = UILabel()
label.text = newWord // variable index, circle to all array
label.font = UIFont(name: "Georgia", size: 28) // change font
label.sizeToFit()
//label.center = CGPoint(x: 180, y: 480)
// find screen size to place new label near add button
var bounds: CGRect = UIScreen.mainScreen().bounds
var width:CGFloat = bounds.size.width
var height:CGFloat = bounds.size.height
label.center = CGPoint(x: width - 100, y: height - 90) // position new label near screen bounds
label.backgroundColor = UIColorFromRGB(0xffffff)
view.addSubview(label)
// Pan Gesture
var panGesture = UIPanGestureRecognizer(target: self, action: Selector("handlePanGesture:"))
label.addGestureRecognizer(panGesture)
label.userInteractionEnabled = true
}
// Add all label in array to the view
func createLabels(){
for word in wordArray {
var label = UILabel()
label.text = word // variable index, circle to all array
label.font = UIFont(name: "Georgia", size: 28)
label.sizeToFit()
//label.center = CGPoint(x: 200, y: 200)
label.backgroundColor = UIColor.whiteColor()
// NOT WORKING attempt to set the random value within screen size... so that label use more space or bigger screen
// var bounds: CGRect = UIScreen.mainScreen().bounds
// var width = bounds.size.width
// var height = bounds.size.height
// var x = CGFloat(arc4random_uniform(width))
// var y = CGFloat(arc4random_uniform(height))
var x = CGFloat(arc4random_uniform(300))
var y = CGFloat(arc4random_uniform(480))
label.center = CGPoint(x: x, y: y)
view.addSubview(label)
// Pan Gesture
var panGesture = UIPanGestureRecognizer(target: self, action: Selector("handlePanGesture:"))
label.addGestureRecognizer(panGesture)
label.userInteractionEnabled = true //because label are not interactive by default, we have to add that
}
}
// store new word from textfield
@IBAction func wordInputChanged(sender: UITextField) {
//println("CA CHANGE")
var newWord = wordInput.text
}
@IBAction func addButtonPressed(sender: AnyObject) {
//println("BUTTON!")
addNewLabel()
}
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