How to add a loading view for apple watch?

2019-06-05 16:19发布

问题:

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

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

回答1:

I have used very simple progress using WKInterfaceLabel,

Create properties and outlets,

@IBOutlet var loadingLabel: WKInterfaceLabel!
var loadingTimer = Timer()
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(InterfaceController.updateProgress), userInfo: nil, repeats: true)

    loadingLabel.setHidden(false)
}

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:
        print("default case")
    }
}

func stopProgressIndicator() {

    loadingTimer.invalidate()
    lastUpdateLabel.setHidden(true)
}

Use these functions to show and hide,

startProgressIndicator()
stopProgressIndicator()