[Swift 1] Day 1 - Code Exercise Solutions


#23

Solution 1: Screen origin is upper left. Note, the label origins are centered (label width/2, label height/2)

Solution 2: Wanted to make the label fall diagonally from upper left to lower right. Had to use label bounds to make sure the “begin” and “end” placement of the label was fully visible on screen.

Before animation:

// make sure the label is fully visible on screen
var beginX = iPhoneLabel.bounds.size.width/2.0
var beginY = iPhoneLabel.bounds.size.height/2.0
iPhoneLabel.center = CGPoint(x:beginX, y:beginY)
iPhoneLabel.alpha = 0.1 // give a hint of visibility to see that it was on screen

Inside Animation Block

//cleaned up springiness with new parameters
UIView.animateWithDuration(2.0, delay: 0.6, usingSpringWithDamping: 0.18, initialSpringVelocity: 0.0, options: nil, animations: {
     // drop to the lower right of the screen – falling diagonally
     // making sure the label's final resting place is fully visible on screen
     var endX = self.view.bounds.size.width - self.iPhoneLabel.bounds.size.width/2.0
     var endY = self.view.bounds.size.height - self.iPhoneLabel.bounds.size.height/2.0
     self.iPhoneLabel.center = CGPoint(x:endX, y:endY)
      self.iPhoneLabel.alpha = 1 // Visible = 1
   }, completion: nil)
}

#24

Top Left
Iphone 4,5, 6, 6+ all have different resolutions. My iPhone 5 is 640×1136 at 326 ppi.

BTW, i tried Gist but I cannot make it appears :-/

//
//  ViewController.swift
//  Boucing Label
//
//  Created by Pascal Andy on 2014-10-07.
//  Copyright (c) 2014 Pascal Andy. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    // 1. Declare ivars
    var myFirstLabel: UILabel!
    var iPhoneLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // 2. Create labels
        myFirstLabel = UILabel()
        iPhoneLabel = UILabel()
        
        // Calls the Method when starting the App
        addLabels()
        
        var TapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTapGesture:"))
        view.addGestureRecognizer(TapGesture)
    }
    
    func handleTapGesture(tapGesture : UITapGestureRecognizer) {
        println("tap")
        
        addLabels()
    }
 
    // 3. Methods
    func addLabels() {
    
        myFirstLabel.text = "Day 1"
        myFirstLabel.font = UIFont.systemFontOfSize(38)
        myFirstLabel.sizeToFit()
        myFirstLabel.center = CGPoint(x: 0, y: 240)
        view.addSubview(myFirstLabel)
        
        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.1, initialSpringVelocity: 0.0, options: nil, animations: {
            
            self.myFirstLabel.center = CGPoint(x: 200, y: 240)
            
            }    , completion: nil)
        
        iPhoneLabel.text = "My first iPhone App"
        iPhoneLabel.font = UIFont.systemFontOfSize(30)
        iPhoneLabel.sizeToFit()
        iPhoneLabel.center = CGPoint(x: 0, y: 290)
        view.addSubview(iPhoneLabel)
        iPhoneLabel.alpha = 0 // invisible
        
        UIView.animateWithDuration(2.0, delay: 0.5, usingSpringWithDamping: 0.1, initialSpringVelocity: 0.0, options: nil, animations: {
            
            self.iPhoneLabel.center = CGPoint(x: 200, y: 290)
            self.iPhoneLabel.alpha = 1 // visible
            
            }, completion: nil)
    
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

#25

Q1: Top Left Corner is 0,0
Q2: 375 x 667px

I also messed around with the font size and damping:
myFirstLabel.text = "My First"
myFirstLabel.font = UIFont.systemFontOfSize(45)
myFirstLabel.sizeToFit()
myFirstLabel.center = CGPoint(x: 0, y: 250)
view.addSubview(myFirstLabel)

    UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: nil, animations: {
        
        self.myFirstLabel.center = CGPoint(x: 0 + 90, y:270)
        
        }, completion: nil)
    
    iPhoneLabel.text = "iPhone App"
    iPhoneLabel.font = UIFont.boldSystemFontOfSize(53)
    iPhoneLabel.sizeToFit()
    iPhoneLabel.center = CGPoint(x: 0, y: 300)
    view.addSubview(iPhoneLabel)
    
    iPhoneLabel.alpha = 0 // Hidden = 0
    
    UIView.animateWithDuration(2.0, delay: 0.4, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: nil, animations: {
        
        self.iPhoneLabel.center = CGPoint(x: 0 + 200, y: 320)
        self.iPhoneLabel.alpha = 1 // Visable = 1
        }, completion: nil)

#26

Questions:

  1. 0.0 is located on the top left corner of the screen
  2. iPhone 6 screen size is 376 points and 667 points

Code exercise:

myFirstLabel.text = "My first"
myFirstLabel.font = UIFont.systemFontOfSize(36)
myFirstLabel.sizeToFit()
myFirstLabel.center = CGPoint(x: 375, y: 333)
view.addSubview(myFirstLabel)

    UIView.animateWithDuration(0.9, delay: 0.0, usingSpringWithDamping: 0.9,
        initialSpringVelocity: 0.0, options: nil, animations: {
            
            self.myFirstLabel.center = CGPoint(x: 100 + 90, y:233)
            
            }, completion: nil)
    
    iPhoneLabel.text = "iPhone app"
    iPhoneLabel.font = UIFont.boldSystemFontOfSize(48)
    iPhoneLabel.sizeToFit()
    iPhoneLabel.center = CGPoint(x: 375, y: 333)
    view.addSubview(iPhoneLabel)
    
    iPhoneLabel.alpha = 0  // Hidden = 0
    
    
    UIView.animateWithDuration(2.0, delay: 0.5, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: nil, animations: {
        
        self.iPhoneLabel.center = CGPoint(x: 100 + 90, y: 333)
        self.iPhoneLabel.alpha = 1 // Visible = 1
        }, completion: nil)

#27

Just wanted to post this if anyone was wondering the same thing.
This is how to do double movement - so the same item moves twice.

//
//  ViewController.swift
//  bouncing labels
//
//  Created by Razvigor Andreev on 10/6/14.
//  Copyright (c) 2014 Razvigor Andreev. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
        // 1. declare ivars
    
    var myFirstLabel: UILabel!
    var iPhoneLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // 2. create the labels
        
        myFirstLabel = UILabel()
        iPhoneLabel = UILabel()
        addLabels()
        
        
        var tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTapGesture:"))
        view.addGestureRecognizer(tapGesture)
        
        }
    
    func handleTapGesture(tapGesture: UITapGestureRecognizer) {
        
        println("tap")
        addLabels()
        }
    
    func addLabels () {
        
        myFirstLabel.text = "My First"
        myFirstLabel.font = UIFont.systemFontOfSize(36)
        myFirstLabel.sizeToFit()
        myFirstLabel.center = CGPoint(x: 100, y: 40)
        view.addSubview(myFirstLabel)
        
        
        UIView.animateWithDuration(0.9, delay: 0.0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.0, options: .CurveLinear, animations: {
            
            self.myFirstLabel.center = CGPoint(x: 100, y:40+200 )
            
            }, completion: nil)
            
            UIView.animateWithDuration(0.6, delay: 0.55, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.0, options: .CurveLinear, animations: {
                
                self.myFirstLabel.center = CGPoint(x: 100, y:130+200 )
            
            
            
            }, completion: nil)
        
        
        
        iPhoneLabel.text = "iPhone App"
        iPhoneLabel.font = UIFont.boldSystemFontOfSize(48)
        iPhoneLabel.sizeToFit()
        iPhoneLabel.center = CGPoint(x: 200, y: 90)
        view.addSubview(iPhoneLabel)
        iPhoneLabel.alpha = 0
        
        UIView.animateWithDuration(2, delay: 0.5, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: nil, animations: {
            
            
            self.iPhoneLabel.center = CGPoint(x: 200, y: 90+200)
            self.iPhoneLabel.alpha = 1
            
            }, completion: nil)
        
    }
    


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

#28

I think I mentioned this in one of the troubleshooting or Q&A videos for Day 1 or Day 2.


#29

Here is my Day1 solution

Please, let me know if you guys find any errors. Thanks!

Regards
Ranjith


#30

Nothing earth shattering in my code solution (see below), but I’m wondering how one found the pixel specs for the iPhones bc the numbers don’t match what I was reading in the iPhone tech-specs listing for the new 6 or 6+?

Code as follows:
iPhoneLabel.text = "iPhone app"
iPhoneLabel.font = UIFont.boldSystemFontOfSize(42)
iPhoneLabel.sizeToFit()
iPhoneLabel.center = CGPoint(x: 500, y: 300)
view.addSubview(iPhoneLabel)
iPhoneLabel.alpha = 0 // Hidden = 0

        UIView.animateWithDuration(2.0, delay: 0.5, usingSpringWithDamping: 0.1, initialSpringVelocity: 0.0, options: nil, animations: {
            
            self.iPhoneLabel.center = CGPoint(x: 200, y: 90 + 200)
            self.iPhoneLabel.alpha = 1 // Visible = 1
            }, completion: nil)
}

#31

Very useful for chaining animations!


#32

What do you see for the numbers?


#33

Code Exercises

Questions

  1. 0,0 are the coordinates to the upper left physical screen
  2. iPhone 6 screen dimensions are 1334px X 750px

#34

Here’s my code: https://gist.github.com/7ff5e22452dab1dee4af.git

For the Questions:
0,0 is at the top left corner of the screen. Similar to OS X. I think it’s different for Windows, which is in the center of the screen. That’s why the mouse starts at different positions when you boot up.
Like Ryan mentioned, the iOS screens are different per device. Which I think is the point of the new style of designing apps that are screen size independent. There are too many different screen sizes to make pixel perfect designs.


#35

0,0 is a the top left corner, a standard.

There are multiple iPhone screen sizes, right now im using an iPhone6 with 750x1334 pixels.

Here is my code - https://gist.github.com/matmac/da8bf7b4994e35c93908


#36

Where can i upload my code


#37

func startAnimation(){

    mylabel.text = "iPhone App"
    mylabel.font = UIFont.systemFontOfSize(40)
    mylabel.sizeToFit()
    mylabel.center = CGPoint(x: 50, y: 300)
    view.addSubview(mylabel)
    mylabel.alpha = 0
    
    UIView.animateWithDuration(3.0, delay: 0.5, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.0, options: nil, animations: {
        
        self.mylabel.center = CGPoint(x: 50 + 135, y: 300)
        self.mylabel.alpha = 1
        
    }, completion: nil)
    
    
}

#38

That works, or you can use a gist from github.

Do what ever is easiest for you.


#39

And is the code ok? Was that what you asked for?

I’m doing second day tomorrow!! Thanks!!!


#40

What happens when you run it?

Do the results show you what you expect?

If so, yes you’re doing it right. If not, try to figure out what’s going wrong.


#41

Can someone explain how for iPhone 6, the stated resolution on Apple’s website is 750x1334, however, here almost everyone says its 375x667? If someone could explain the scaling concept to me like I’m 5, that’d be great!


#42

I surfed around and I think I have my answer, which is very well explained here: http://www.paintcodeapp.com/news/iphone-6-screens-demystified