user1872384
user1872384

Reputation: 7127

How to add a loading view for apple watch?

I want to display a loading view (the one provided by apple) once a WKInterfaceButton is pressed:

1

I need this because after the WKInterfacebutton is pressed, I'm calling the main iPhone app to do some service calls which will take some time to return a response.

    WKInterfaceController.openParentApplication(watchMessage, reply: { (reply:[NSObject : AnyObject]!, error: NSError!) -> Void in

Upvotes: 4

Views: 4528

Answers (1)

Dhaval Panchal
Dhaval Panchal

Reputation: 2529

I have used very simple progress using WKInterfaceLabel,

Create properties and outlets,

@IBOutlet private var loadingLabel: WKInterfaceLabel!
private var loadingTimer = Timer()
private var progressTracker = 1

Implementation,

func startProgressIndicator() {
    // Reset progress and timer.
    progressTracker = 1
    loadingTimer.invalidate()

    // Schedule timer.
    loadingTimer = Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)

    loadingLabel.setHidden(false)
}

@objc
private func updateProgress() {
    switch progressTracker {
    case 1:
        lastUpdateLabel.setText("Loading..")
        progressTracker = 2
    case 2:
        lastUpdateLabel.setText("Loading...")
        progressTracker = 3
    case 3:
        lastUpdateLabel.setText("Loading.")
        progressTracker = 1
    default:
        break
    }
}

func stopProgressIndicator() {
    loadingTimer.invalidate()
    lastUpdateLabel.setHidden(true)
}

Use these functions to show and hide,

startProgressIndicator()
stopProgressIndicator()

Upvotes: 6

Related Questions