[Mobile Lab] Week4: Motion App

This week’s assignment was creating an iOS app that has more than one motion element.
I wanted to make a cooler app though I was terribly busy and tired these days, so I created a rather simple one.
It was inspired by a slingshot. If you press the ‘Fire’ button, the circle in the middle of the screen is emitted.
Although the ball’s Y direction is always towards zero (upper edge of the screen), the X direction that the ball aims to is always determined by the random function.
You can initialize the position of the ball by hitting the ‘Reset’ button. Most importantly, it has a spring motion as if it is linked to a rubber band.
Following is the code in ViewController.swift

import UIKit

class ViewController: UIViewController, UIGestureRecognizerDelegate {
    
    // UIView to animate.
    var circleView: UIView!
    let scale: CGFloat = 1.0;
    
    let duration: Double = 1.0
    
    // For view sizing.
    let radius: CGFloat = 50.0
    
    let colour = UIColor.white

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        circleView = UIView(frame: CGRect(x: 0, y: 0, width: radius * 2, height: radius * 2))
        circleView.layer.cornerRadius = radius
        
        // Set the circle color.
        circleView.backgroundColor = colour
        
        // Center on parent view.
        circleView.center = self.view.center
        
        
        // Add to parent view.
        self.view.addSubview(circleView)
        
 
        
    }
    
    @IBAction func reset(_ sender: UIButton) {
        circleView.center  = self.view.center
    }
    
    
    @IBAction func buttonPressed(_ sender: UIButton) {
        
        let xTarget : CGFloat = CGFloat(arc4random_uniform(UInt32(self.view.frame.width))) //self.view.frame.width / 3.0
        
        UIView.animate(
            withDuration: duration,
            delay: 0,
            usingSpringWithDamping: 0.20,
            initialSpringVelocity: 0.0,
            options: [.curveEaseInOut],
            animations: {
                self.circleView.center = CGPoint(x: xTarget, y: 0)
            }
        )
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

You can find the project at the following link: https://github.com/yeony102/mobilelab2018/tree/master/Week04

I have also uploaded this app to iTunse Connect and added my Mobile Lab classmates as my app users.

Leave a Reply

Your email address will not be published. Required fields are marked *