如何根据信标的距离刷新背景图像?

时间:2019-05-16 09:49:37

标签: swift xcode beacon proximity

我想根据信标的接近程度更改应用程序的背景图像。如果我在iPhone上运行该应用程序(请参见下面的代码),则它会根据接近程度正确显示背景图片-但是,如果我移动,它不会更改/刷新背景图片。 我尝试使用背景色进行相同操作(请参见下面的代码),并且可以正常工作:该应用程序根据信标的接近程度更改其背景色,并且如果我正确移动它,则可以更改/刷新背景色。只是背景图片无法正常工作。

如何强制背景图像正确刷新?

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()#>
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  • 您要以零索引插入所有图像,首先删除上一张图像,就好像它包含任何图像一样,例如在接近度之间切换会插入许多全部为零的图像,最后一张将出现。或者只是将其添加为子视图并显示在前面(如果需要),而不是插入零。
    • 颜色有效,因为您查看的颜色只有一种。图像一一追加。为了更好地理解,请尝试查看调试。