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

use*_*384 2 swift watchkit

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

1个

我需要这样做,因为在按下WKInterface按钮后,我正在调用主iPhone应用程序进行一些服务调用,这将需要一些时间来返回响应。

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

Dha*_*wal 5

我使用WKInterfaceLabel的进度非常简单,

创建属性和出口,

@IBOutlet var loadingLabel: WKInterfaceLabel!
var loadingTimer = Timer()
var progressTracker = 1
Run Code Online (Sandbox Code Playgroud)

实施,

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)
}
Run Code Online (Sandbox Code Playgroud)

使用这些功能可以显示和隐藏,

startProgressIndicator()
stopProgressIndicator()
Run Code Online (Sandbox Code Playgroud)

  • 该解决方案应有助于将OP指向正确的方向,并且至少该响应者足以提供一些代码-(抱怨的人没有!)。另外,OP的问题也没有说Swift(尽管它确实显示了一行代码并使用了Swift标签) (6认同)
  • 您错过了重点。OP可能*不知道* Objective-C。 (3认同)
  • 这里应该没有理由投反对票。如果有人知道快速版本,那么他会在上面加上答案。 (2认同)
  • 嗨@DH14-SL,感谢您提供如此全面的答案……我可以用 obj-c 和 swift 编写……阅读代码没有任何问题。我决定使用图像进行加载以减少编码工作。 (2认同)