jonjamster
3/6/2018 - 12:09 PM

Shake phone code to update random image chosen from an array

import UIKit

class ViewController: UIViewController {
    
    let ballArray = ["ball1", "ball2", "ball3", "ball4", "ball5"]

    var randomBallNumber = 0

    
    @IBOutlet weak var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
       newBallImage()
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // shake code start
    
    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
       newBallImage()
    }
    
    // shake code end
    
    
    @IBAction func askButtonPressed(_ sender: UIButton) {
        
      newBallImage()
        
    }
    

    func newBallImage(){
        
        randomBallNumber = Int(arc4random_uniform(5))
        imageView.image = UIImage(named: ballArray[randomBallNumber])
        
    }
    
  
    
}