[Swift 1] Day 1 - Code Exercise Solutions


#63

What should I do if I wanted the label to bounce multiple times with decreasing height?


#64

Well…a few things :smile:

add a variable that will store your number of bounces and an index:

var bounces: Int = 5
var index: Int!
let decreaseHeight: CGFloat = 30

then do this :smile:

for index = 0; index < bounces, ++index {

// your bounce code here, but using the decreaseHeight param:
//something like:
...
self.myFirstLabel.center = CGPoint(x: 100, y: 200 - (decreaseHeight*index) ) // first time is gonna be 30*0, then -30*1, 30*2... and so on...
....

}

P.S. - just read through this, there are other ways to do it:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html


#65

The 750 X 1334 is in pixels. The 375 X 667 is in points. There are 2.0 pixels per point on the iPhone 6.


#66

Excercise 1
I set the starting coordinates of the label to be:

iPhoneLabel.center = CGPoint(x: -200, y: 90+200)

and the new coordinates to be:

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

Excercise 2
I made changes to the usingSpringWithDamping and initialSpringVelocity properties.

Question 1
The 0,0 coordinate is the top-left of the screen

Question 2
It depends on the device screen size but i used the following code to discover it:

    println("iPhone width (x)")
    println((view.center.x)*2)
    println("iPhone width (y)")
    println((view.center.y)*2)

#67

In regards to your question about the value of the negative X coordinate, I think it only has to be equal to the UILabel’s width.
e.g. If the UILabel was 100px wide, a starting x coordinate of -100 should put it just out of view.

However, I couldn’t find where to get the width property of a UILabel. Maybe it doesn’t have one when we use sizeToFit() ?

Edit I found it! :slight_smile:

iPhoneLabel.center = CGPoint(x: -(iPhoneLabel.bounds.width), y: 90+200)

#68

Made some changes for it come in from the right then in from right and down. So there are 3 motions. Then I added 5 different variations to the animation so they get different looks on each motion. Link to code is below.

0,0 is in the upper left of the screen.
I used these calls to get the screen width, so it would work on different screen sizes.

width = UIScreen.mainScreen().applicationFrame.size.width
height = UIScreen.mainScreen().applicationFrame.size.height


#69

Hi there!

  1. 0, 0 is on top left side of the iPhone screen
  2. iphone 6 screen is 750×1334 pixels

Here is the code I changed inside the addLabels() function:


// iPhoneLabel.center = CGPoint(x: 200, y: 90)
iPhoneLabel.center = CGPoint(x: -200, y: 290)
view.addSubview(iPhoneLabel)

    iPhoneLabel.alpha = 0

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

// self.iPhoneLabel.center = CGPoint(x: 200, y: 90+200)
self.iPhoneLabel.center = CGPoint(x: 200, y: 290)
self.iPhoneLabel.alpha = 1

        }, completion: nil)


#70

Hi

These are the lines of code I changed.

iPhoneLabel.center = CGPoint(x: 300, y: 40 + 200)

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

  1. 0,0 is at the top left corner of the iphone screen
  2. 1334 ×750 - iphone 6, iphone 6 Plus - 1920 x 1080

#71

@ravenshore @GJZ

If you’re looking for a physical simulation (physics), you wouldn’t use the normal animation. You would want to use the UIDynamics.

https://www.bignerdranch.com/blog/uidynamics-in-swift/


#72

//
// ViewController.swift
// labelbounce
//
// Created by Adrian Burns on 2/06/2015.
// Copyright © 2015 Adrian Burns. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

// 1. declare ivars
var myfirstlabel: UILabel!
var iphonelabel: UILabel!
var itworkslabel: 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()
    itworkslabel=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)
    myfirstlabel.alpha=0.1
    
    UIView.animateWithDuration(6.0, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.0, options: nil, animations: {
        
        self.myfirstlabel.center = CGPoint(x:100,y:40+200)
        self.myfirstlabel.alpha=1
        
        }, 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(4.0, delay: 0.6, usingSpringWithDamping: 0.1, initialSpringVelocity: 0.0, options: nil, animations: {
        
        self.iphonelabel.center=CGPoint(x: 200, y: 90+200)
        self.iphonelabel.alpha = 1
        
        }, completion: nil)
    
    itworkslabel.text = "...and it works!"
    itworkslabel.font=UIFont.systemFontOfSize(16)
    itworkslabel.sizeToFit()
    itworkslabel.center=CGPoint(x:-50,y:350)
    
    view.addSubview(itworkslabel)
    itworkslabel.alpha=0.1
    
    UIView.animateWithDuration(3.0, delay: 6.5, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.0, options: nil, animations: {
        
        self.itworkslabel.center = CGPoint(x:-50+350,y:350)
        self.itworkslabel.alpha=0.4
        
        }, completion: nil)

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

}

// Answers to questions:
// coord (0,0) relates to top left of screen.
// iphone (6plus) 1080x1920


#73

//
// ViewController.swift
// Bouncing Labels
//
// Created by Oleg Latypov on 6/6/15.
// Copyright © 2015 Oleg Latypov. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

// Step-1 Declare iVars
var myFirstLabel: UILabel!
var iPhoneLabel: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()
    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 label"
    myFirstLabel.font = UIFont.systemFontOfSize(36)
    myFirstLabel.sizeToFit()
    myFirstLabel.textColor = UIColor(red: 0.4, green: 0.004, blue: 0.52, alpha: 1.0)
    myFirstLabel.center = CGPoint(x: 150, y: 80)
    
    view.addSubview(myFirstLabel)
    UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.0, options: nil, animations: {
        self.myFirstLabel.center = CGPoint(x: 150, y: 80 + 120)
        
        }, completion: nil)
    
    iPhoneLabel.text   = "iPhone app"
    iPhoneLabel.font = UIFont.boldSystemFontOfSize (48)
    iPhoneLabel.textColor = UIColor(red: 0.4, green: 0.3, blue: 0.52, alpha: 0.6)
    iPhoneLabel.sizeToFit()
    iPhoneLabel.center = CGPoint(x: 0, y: 250)
    
    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: 200, y: 250)
        self.iPhoneLabel.alpha = 1
        }, completion: nil )
    
}


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

//==================//
Add new color ;
iPhoneLabel.textColor = UIColor(red: 0.4, green: 0.3, blue: 0.52, alpha: 0.6)

}


#74

Code Exercise

  1. Done
  2. Done

Questions:

  1. Left top corner
  2. for iPhone 6 Pluse is 1242 x 2208 (414 x 736 points)

#75

My answer is as follow:

  1. (0,0) is top left hand corner of the iPhone.
  2. Depending on the iPhone type, the screen in pixels are as followed:

1920-by-1080-pixel resolution at 401 ppi - 6 plus

1334-by-750-pixel resolution at 326 ppi - 6

1136-by-640-pixel resolution at 326 ppi - 5S

1136-by-640-pixel resolution at 326 ppi - 5C

For the code I change the following two lines and its values:


   iPhoneLabel.center = CGPoint(x: 400, y: 300)
   self.iPhoneLabel.center = CGPoint(x: 200, y: 300)


#76

Exercise 1: I added a negative in the starting position

Exercise 2: I played around with the settings in the usingspringwithdampening from 0.1 -0.9 for differing effects.

Q1 : 0,0 is in the top left corner.
Q2 : Really not sure, have read all the comments in the thread and found the above from k281 to be clear and have marked this as something to read up on and understand more.

1920-by-1080-pixel resolution at 401 ppi - 6 plus
1334-by-750-pixel resolution at 326 ppi - 6
1136-by-640-pixel resolution at 326 ppi - 5S
1136-by-640-pixel resolution at 326 ppi - 5C


#77

On OSX, most views use the PDF standard of bottom left corner; you will often find ‘flipped’ coordinates which start in the top left corner.


#78

I did the lazy calculation:
myFirstLabel.center = CGPoint(x: -500, y: 40) (respectively x: 500) - this places the label outside the bounds of the screen and lets it slide in.

Ideally, you’d use [left edge of view - half the width of the label] or right edge of view + half the width of the label] to have them start just outside the bounds of the view.


#79

The 0.0 position is on the left corner

The resolution from iPhone 6 is 750x1334 pixels

To change the slide from top to bottom to left to right, just have to change the CGPoint coordinates, keeping the y coordinate to a fixed value and change the x coord.

To change the physics just have to change the parameters of animateWithDuration, changing delay, duration, etc


#80

1- (0,0) coordinative is at the left top of the screen.
2- The emulator screen size is 320x568 set on iPhone 5.

I made the “iPhone app” label to slide from left to right like this:

self.iPhoneLabel.center = CGPoint(x: 80 + 80,y: 290)

I really don’t understand the second code exercise.


#81

Set iPhoneAppLabel starting point from off-screen right:
iPhoneAppLabel.center = CGPoint(x: iPhoneAppLabel.bounds.width / 2 + view.bounds.width, y: 290)


#82
iPhoneLabel.text = "iPhone App"
iPhoneLabel.font = UIFont.systemFontOfSize(48)
iPhoneLabel.sizeToFit()
iPhoneLabel.center = CGPoint(x: 0, y: 300)
        
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: 300)
  self.iPhoneLabel.alpha = 1
            
}, completion: nil)
  1. Top left corner
  2. (375.0, 667.0)