如何为 Apple Watch 添加加载视图?

How to add a loading view for apple watch?

我想在按下 WKInterfaceButton 后显示一个加载视图(苹果提供的视图):

我需要这个,因为在按下 WKInterface 按钮后,我正在调用主 iPhone 应用程序来执行一些服务调用,这需要一些时间才能 return 响应。

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

我使用WKInterfaceLabel的过程非常简单,

创建属性和销售点,

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

实施,

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

使用这些函数来显示和隐藏,

startProgressIndicator()
stopProgressIndicator()