[Swift 1] Day 1 - Code Exercise Solutions


#1

Please share your code or answers with your classmates for the Day 1 Code Exercise and related Notebook Activity questions.

Day 1 Course Videos
Day 1 Code Exercise

Share code using


#2
  1. Top left corner is 0.0.
  2. Native resolution of the iPhone 6 is 750 x 1334 but the effective
    resolution is 375 x 667px if we account for retina display.

#3

Code exercice :

  1. animate on x axis changing the x coordinates :
self.myFirstLabel.center = CGPoint(x:100 + 50, y:40) 
  1. Quite confused by the question : changing the springiness AS the label animate. If you mean dynamical change as the animation occurs … i didn’t figure it out.
    If its only playing with the usingSpringWithDamping property, ok done.

Questions :

  1. top left corner is the 0,0 point.
  2. the iphone 6 in simulator gives a 375x667 pt
    found with that instruction :
println("\(view.bounds.size)")

#4

@Paradisiak

No, I didn’t mean change it mid-animation. You can alter the numbers 0.5 for the dampening or initial velocity to change the behavior like I did between the two labels.

Great work finding the size!


#5

I think this is useful if you want to do anything after the animation is completed

If you want to write a code after the animation is finished you will need to write it after the completion parameter in the UIView.animateWithDuration, however, and for some reason the complier expects that the expression will return a boolean (true or false). And to overcome this, you will need to write something like this:

UIView.animateWithDuration(2.0, delay: 0.5, usingSpringWithDamping: 0.1, initialSpringVelocity: 0.0, options: nil, animations: {

        self.iPhoneLabel.center = CGPoint(x: 100 + 100, y: 90)
        self.iPhoneLabel.alpha = 1 // Visibile = 1
        
        }, completion: {
            finished in if(finished){
                println("End of Animation")
            }
    })

#7

self.iphoneLabel.center = CGPoint(x: 300-100, y: 40)

brings from right to left


#8
  1. Instead of “self.iPhoneLabel.center = CGPoint (x:100 , y: 200+40)”

    write “self.iPhoneLabel.center = CGPoint (x:100 + 90, y: 200)”

  2. Done

Questions: 1) Top left corner is 0.0: change the coordinates of one CGPoint to 0.0 and launch the simulator to see where the label is displayed.

              2) 375 x 667

#9

Code

    super.viewDidLoad()
    myFirstLabel = UILabel()
    myFirstLabel.text = "My first"
    myFirstLabel.font = UIFont.systemFontOfSize(36)
    myFirstLabel.sizeToFit()
    myFirstLabel.center = CGPoint(x: 200, y: 240)
    view.addSubview(myFirstLabel)

    UIView.animateWithDuration(0.5, delay: 0.1, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.0, options: nil, animations: {
            self.myFirstLabel.center = CGPoint(x: 200 - 100, y: 240)
        },

    completion: nil)

    iPhoneLabel = UILabel()
    iPhoneLabel.text = "iPhone App"
    iPhoneLabel.font = UIFont.boldSystemFontOfSize(48)
    iPhoneLabel.sizeToFit()
    iPhoneLabel.center = CGPoint(x: 400, y: 290)
    view.addSubview(iPhoneLabel)
    iPhoneLabel.alpha = 0

    UIView.animateWithDuration(2.0, delay: 0.6, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: nil, animations: {

        self.iPhoneLabel.center = CGPoint(x: 400 - 200, y: 290)
        self.iPhoneLabel.alpha = 1

        }, completion: nil)

Questions

  1. Top-left corner is 0,0
  2. 375x667

#10

I did have a problem initially with not seeing any text. I finally discovered that it was because the size of the simulator virtual screen was larger than that of the screen it was showing (that it is is longer), and the text was at the top of the screen and the screen was scrolled down to the bottom. I had to scroll down to see the text. It is still that way.

Can you possibly comment on that at some time, if you get the opportunity


#11

I faced the same problem, The solution is:

  1. Make sure you are selecting iPhone 5 in both Xcode (This is found beside the Play Stop button on the up left corner) and the Simulator (from the menu, Hardware -> Devices -> iPhone 5)
  2. From the simulator menu, select Window - > Scale -> 50%

This should solve it.


#13

I think we’ve had a few answers posted but here is my addLabels() method that animates from right to left.

    func addLabels() {
    myFirstLabel.text = "My First"
    myFirstLabel.font = UIFont.systemFontOfSize(36)
    myFirstLabel.sizeToFit()
    myFirstLabel.center = CGPoint(x: 200, y: 240)
    view.addSubview(myFirstLabel)
    
    UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.0, options: nil, animations: {
        self.myFirstLabel.center = CGPoint(x: 200 - 100, y: 240)
        
        }, completion: nil)
    
    iPhoneLabel.text = "iPhone App"
    iPhoneLabel.font = UIFont.boldSystemFontOfSize(42)
    iPhoneLabel.sizeToFit()
    iPhoneLabel.center = CGPoint(x: 400, y: 290)
    view.addSubview(iPhoneLabel)
    
    iPhoneLabel.alpha = 0
    
    UIView.animateWithDuration(2.0, delay: 0.5, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: nil, animations: {
        self.iPhoneLabel.center = CGPoint(x: 400 - 200, y: 290)
        self.iPhoneLabel.alpha = 1
        }, completion: nil)
}

#14
  1. 0,0 is top left corner
  2. Screen size of iPhone 6 is 376 x 667 pixels

#15

1.- Where is the (0,0) coordinate in relation to the iphone scree: At the very left top of the iPhone screen.
2.- How big is the iPhone screen in pixels: iPhone 6 Retina display is 1334 x 750 (pixels)

func addLables (){
myFirstLabel.text = "My first"
myFirstLabel.font = UIFont.systemFontOfSize(30)
myFirstLabel.sizeToFit()
myFirstLabel.center = CGPoint (x: 162, y: 40)
myFirstLabel.textColor = UIColor.grayColor()
view.addSubview(myFirstLabel)
myFirstLabel.alpha = 0

    UIView.animateWithDuration(1.9, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.6, options: nil, animations: {
        
        self.myFirstLabel.center = CGPoint (x: 162, y: 45 + 200)
        self.myFirstLabel.alpha = 1
        }, completion: nil)
    
    
    iPhoneLable.text = "iPhone @pp"
    iPhoneLable.font = UIFont.boldSystemFontOfSize(48)
    iPhoneLable.sizeToFit()
    iPhoneLable.center = CGPoint (x:370, y:270)
    iPhoneLable.textColor = UIColor.grayColor()
    view.addSubview(iPhoneLable)
    iPhoneLable.alpha = 0
    
    
    UIView.animateWithDuration(2.2, delay: 0.7, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.1, options: nil, animations: {
        self.iPhoneLable.center = CGPoint (x: 180 , y:270)
        self.iPhoneLable.alpha = 1
        }, completion: nil)

}

#16

I added a new “By Jordan M” to slide in from the left side of the screen. Here is my new func 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.5, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.0, options: nil, animations: {
        self.myFirstLabel.center = CGPoint(x: 100, y: 40+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 //Hidden
    
    UIView.animateWithDuration(2.0, 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 //Visible
        }, completion: nil)
    
    byJordanLabel.text = "by Jordan M"
    byJordanLabel.font = UIFont.boldSystemFontOfSize(12)
    byJordanLabel.sizeToFit()
    byJordanLabel.center = CGPoint(x: 0, y: 350)
    view.addSubview(byJordanLabel)
    
    byJordanLabel.alpha = 0 //Hidden
    
    UIView.animateWithDuration(1.0, delay: 1.5, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.0, options: nil, animations: {
        self.byJordanLabel.center = CGPoint(x: 0+200, y: 350)
        self.byJordanLabel.alpha = 1 //Visible
        }, completion: nil)
    
}

#17

Make the “iPhone App” label slide in from the right or left side.

Left to right

Start position:

iPhoneLabel.center = CGPoint(x: 0, y: 90)

End position:

self.iPhoneLabel.center = CGPoint(x: 190, y: 90)

Change the physics for the springiness of the labels as they animate.

...
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.0,
...

Where is the (0,0) coordinate in relation to the iPhone screen?

(0,0) is the top left corner but SPOILER ALERT in Sprite Kit it’s not:

:wink:

How big is the iPhone screen in pixels (or points)?

println("Size in points: \(view.bounds.size)")
// 320 x 568 pts which represents
// 640 x 1136 pixels
//
// On a standard-resolution screen, the scale factor is typically 1.0.
// On a high-resolution screen, the scale factor is typically 2.0.
// In the future, other scale factors may also be possible.
// (In iOS prior to version 4, you should assume a scale factor of 1.0.)

Reference on Pixels vs Points

(Good idea on the view.bounds.size, @Paradisiak)


#18
  1. Top left corner
  2. 376 x 667

Here’s what I ended up doing, “My first” coming up from the bottom, and “iPhone app” from the right.

func addLabels() {
    
    myFirstLabel.text = "My First"
    myFirstLabel.font = UIFont.systemFontOfSize(36)
    myFirstLabel.sizeToFit()
    myFirstLabel.center = CGPoint(x: 100, y: 700)
    view.addSubview(myFirstLabel)
    
    UIView.animateWithDuration(3.0, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: nil, animations: {
        
        self.myFirstLabel.center = CGPoint(x: 100, y: 240)
        
        }, completion: nil)
    
    iPhoneLabel.text = "iPhone app"
    iPhoneLabel.font = UIFont.boldSystemFontOfSize(48)
    iPhoneLabel.sizeToFit()
    iPhoneLabel.center  = CGPoint(x: 400, y: 290)
    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: 200, y: 200 + 90)
        self.iPhoneLabel.alpha = 1 // Visible = 1
        }, completion: nil)
    
}

#19

Questions
Top Left of Screen (x=0, y=0)
Screen Size: 357px by 667px
Added my initials sweeping in from the left. Timing needs to be tweaked but I am happy with my first attempt.

Link to first lesson results:


#20

jamilhasan - thank you - worked for me as well. There must also be something to tweak in the main.storyboard - Utilities - Size Inspector pane. I hope that Paul will touch on this at some time


#21

Hey Paul,

here is the link of my First Exercise. I have added some more things like reverse animation and y position set randomly between 100-700 on touch

Click to download Excersice File

Also here is answer :

  1. (0,0) coordinate on extremely top left corner.
  2. iPhone 6 Simulator screen size (375.0,667.0)
    iPhone 6 Plus Simulator screen size (414.0,736.0)

#22

Here is my code…

class ViewController: UIViewController {

// 1. Declare ivars (instance variables)
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") // to test if tap gesture is working
    addLabels()
}

func addLabels(){
    
    myFirstLabel.text = "My first"
    myFirstLabel.font = UIFont.systemFontOfSize(36)
    myFirstLabel.sizeToFit()
    myFirstLabel.center = CGPoint(x: 900, y: 160)
    view.addSubview(myFirstLabel)
    
    // add animation effects
    
    UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: nil, animations: {
        
        self.myFirstLabel.center = CGPoint(x: 100, y: 160)
        
        }, completion: nil)
    
    
    iPhoneLabel.text = "iPhone App"
    iPhoneLabel.font = UIFont.boldSystemFontOfSize(48)
    iPhoneLabel.sizeToFit()
    iPhoneLabel.center = CGPoint(x: -200, y: 200)
    view.addSubview(iPhoneLabel)
    
    iPhoneLabel.alpha = 0 // Hidden = 0
    
    UIView.animateWithDuration(2.0, delay: 0.5, usingSpringWithDamping: 0.2, initialSpringVelocity: 10.0, options: nil, animations: {
        
        self.iPhoneLabel.center = CGPoint(x: 200, y: 200)
        self.iPhoneLabel.alpha = 1 // Visible = 1
        
        }, completion: nil)
    
    
}