动画完成时隐藏 UIProgressView

Hide UIProgressView when animation is completed

我正在使用 CKOperation 将记录数组保存在云工具包中,如下所示并使用进度视图显示进度。

    let saveOperation = CKModifyRecordsOperation(recordsToSave: ckRecord, recordIDsToDelete: nil)

    saveOperation.perRecordProgressBlock = {
        record, progress in
        if progress >= 1 {
            self.completedRecord.addObject(record)
            let totalSavedRecord = Double(self.completedRecord.count)
            let totalProgress = totalSavedRecord / self.totalStudent
            let percentageProgress = Float(totalProgress * 100)
            progressView.setProgress(percentageProgress, animated: true)

            println(progress)
            println(progressView.progress)
            println(percentageProgress)

        }
    }

我想在进度达到 100% 且动画完成时隐藏进度视图。

目前我很快就达到了 percentageProgress 到 100.0,但是进度视图动画出现了一些延迟。如果我在 percentageProgress 达到 100.0 时隐藏,那么我将永远看不到任何动画。

全程进度值为1.0。

progressView.progress 的值始终也是 1.0。

我想再次显示从 0% 到 100% 的完整动画,然后才隐藏进度视图。

CloudKit 回调块在后台线程上执行。当您更新 UI 时,您应该在主线程上进行。否则你会看到像这样的奇怪延迟。尝试将您的代码包装在这样的块中:

NSOperationQueue.mainQueue().addOperationWithBlock {
   progressView.setProgress(percentageProgress, animated: true)
}

这对我有用。您只需要在处理完成时调用此函数即可。

func resetProgressView() {
    let TIME_DELAY_BEFORE_HIDING_PROGRESS_VIEW: UInt32 = 2
    // Wait for couple of seconds so that user can see that the progress view has finished and then hide.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
        sleep(TIME_DELAY_BEFORE_HIDING_PROGRESS_VIEW)
        dispatch_async(dispatch_get_main_queue(), {
        self.progressView.setProgress(0, animated: false)     // set the progress view to 0
        })
    })
}