Messages to indicate when moving from one proximity to another

I am creating a treasure hunt app, and I would like to indicate to users whether they are getting closer (warmer) or further away (colder) from the beacon. I am currently using if statements to show messages based on proximity, so when the user is near, the message ‘warmer’ appears, and when they are far, the message ‘colder’ appears. This isn’t ideal as, for example, if the user is heading towards the beacon but their proximity is still far, the message shows that they are ‘cold’, which may cause them to change direction and move away from the beacon.

Ideally, I would to show the following types of messages:

entering region - ‘warmer’,
far to near - 'hot’
near to far - 'colder’
far to outside region - ‘freezing’

Can this be done?

Absolutely—you just need to have your if statement look not only at the current state (i.e., far/near/etc.), but also at the previous state. For example, in pseudo-code:

currentState = beacon.proximityZone

if previousState == far && currentState == near {
    "hot"
} else if previousState == near && currentState == far {
    "colder"
} else …

previousState = currentState

…

Thanks for your reply, but how would I determine the previous state in the first place?

For the very first ranging event, you won’t have it (e.g., make it null), but then for each consecutive didRangeBeacons invocation you will. Just assign it to some property at the end of the ranging event, like shown in the pseudo-code above.

Something like that: (Swift-ish pseudo-code)

let previousState: CLProximity?

func didRangeBeacons(beacons) {
    currentState = beacons.first?.proximityZone

    if previousState == far && currentState == near {
        "hot"
    } else if previousState == near && currentState == far {
        "colder"
    } else …

    self.previousState = currentState
}

Well after a few days away from the app, I’ve finally managed to get it working, thank you! I just have one more question: How do I get didEnterRegion and didExitRegion to work whilst the app is in the foreground? Everything I can find online talks about displaying alerts or notifications. I want to tie it in with the treasure hunt app as above, so for example:
if didEnterRegion = true
{
textField = “Getting warmer”
}

if didEnterRegion = true
{
textField = “Freezing”
}

or is there another why of indicating when a region has been entered/exited?