swiftxcodebeaconproximity

How to refresh a background image depending on the proximity of a beacon?


I want to change the background image of my app, depending on the proximity of a beacon. If I run the app (see code below) on my iPhone, it shows the background image correctly depending on the proximity - but if I move, it doesn't change/refresh the background image. I tried the same with a background color (see code below) and it worked: the app changes its background color depending on the proximity of the beacon, and if I move it correctly changes/refreshes the background color. Just the background image doesn't work properly.

How can I force the background image to refresh correctly?

func updateDistance(_ distance: CLProximity) {
    UIView.animate(withDuration: 0.8) {
        switch distance {
        case .unknown:
            let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
            backgroundImage.image = UIImage(named: "1.jpg")
            //backgroundImage.reloadInputViews()
            backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
            self.view.insertSubview(backgroundImage, at: 0)
        //print("nothing here..")
        case .far:
            let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
            //backgroundImage.image = UIImage(named: "2.png")
            backgroundImage.image = UIImage(named: "3.png")
            //backgroundImage.reloadInputViews()
            backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
            self.view.insertSubview(backgroundImage, at: 0)
        case .near:
            let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
            backgroundImage.image = UIImage(named: "4.png")
            //backgroundImage.reloadInputViews()
            backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
            self.view.insertSubview(backgroundImage, at: 0)
        case .immediate:
            let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
            backgroundImage.image = UIImage(named: "5.jpg")
            //backgroundImage.reloadInputViews()
            backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
            self.view.insertSubview(backgroundImage, at: 0)
        @unknown default:
            // fatalError()
            let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
            backgroundImage.image = UIImage(named: "6.jpg")
            //backgroundImage.reloadInputViews()
            backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
            self.view.insertSubview(backgroundImage, at: 0)
        }
    }





func updateDistance(_ distance: CLProximity) {
    UIView.animate(withDuration: 0.8) {
        switch distance {
        case .unknown:
            self.view.backgroundColor = UIColor.gray
            print("nothing here..")
        case .far:
            self.view.backgroundColor = UIColor.blue
        case .near:
            self.view.backgroundColor = UIColor.orange
        case .immediate:
            self.view.backgroundColor = UIColor.red
//          self.sendUserNotification()
        @unknown default:
            <#fatalError()#>
        }
    }
}

Solution