swift - 如何为 Apple Watch 添加加载 View ?

标签 swift watchkit

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

1

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

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

关于swift - 如何为 Apple Watch 添加加载 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29337450/

相关文章:

ios - 快速将 mp3 文件转换为 URL

ios - 在同一个 ViewController 中打开文本文件

ios - -[AXSpeechAction 保留] : message sent to deallocated instance 0x1c37e2b0

ios - 类消息的接收者类型 "WkAlertAction"是前向声明

ios - Watchkit map : show current location

ios - WKInterfaceMap 从不加载

ios - 获取地点

swift - 有没有一种干净的方法可以将 CNPostalAddress 转换为 CNMutablePostalAddress?

ios - 在 WatchKitExtension 中混合 swift 和 objective-c

location - 我可以让 Watch 应用在后台运行吗?