gpt4 book ai didi

ios - 一旦异步调用在 swift 中出现,就停止它

转载 作者:行者123 更新时间:2023-11-30 13:27:11 24 4
gpt4 key购买 nike

我的这个loadingOverlay 单例版本有一些问题。

应该发生的事情是它出现在屏幕上,并带有一个 View 和一个带有文本“正在加载,请稍候”的标签。或类似的东西。然后,如果加载时间超过 2 秒(我已将其更改为 10 秒以进行调试),则文本将更改为随机的可爱短语。

首先,应该改变文本的动画似乎没有发生。相反,文本会立即改变。

更重要的是,如果由于某种原因,我的异步调用 block 被执行多次,我只希望最近一次调用它运行,并且我希望它的先前实例在运行之前终止。

我正在阅读有关回调和 promise 的内容,它们看起来很有希望。这是一个可以快速遵循的模式吗?

顺便说一句,在我学习swift和iOS的过程中,我一直在尝试,我尝试过[unowned self]现在我正在尝试[弱 self ],但我不太确定哪个在这里最合适。

// from http://stackoverflow.com/questions/33064908/adding-removing-a-view-overlay-in-swift/33064946#33064946

import UIKit

class LoadingOverlay{
static let sharedInstance = LoadingOverlay()

//above swifty singleton syntax from http://krakendev.io/blog/the-right-way-to-write-a-singleton

var overlayView = UIView()

var spring: CASpringAnimation!
var springAway: CASpringAnimation!

var hidden = false

private init() {} //This line prevents others from using the default () initializer for this class

func setupSpringAnimation(startY: CGFloat, finishY: CGFloat) {
overlayView.layer.position.y = startY
spring = CASpringAnimation(keyPath: "position.y")
spring.damping = 10
spring.fromValue = startY
spring.toValue = finishY
spring.duration = 1.0
spring.fillMode = kCAFillModeBackwards


}

func showOverlay() {
print("show overlay")
overlayView.alpha = 1
hidden = false
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate,
let window = appDelegate.window {

setupSpringAnimation(-window.frame.height / 2, finishY: window.frame.height / 2)
let overlayViewFramesize = 0.65 * min(window.frame.height, window.frame.width)
overlayView.frame = CGRectMake(0, 0, overlayViewFramesize, overlayViewFramesize)
overlayView.center = window.center
overlayView.backgroundColor = UIColor.greenColor()
overlayView.clipsToBounds = true
overlayView.layer.cornerRadius = overlayViewFramesize / 8

let label = UILabel(frame: CGRectMake(0,0,overlayViewFramesize * 0.8 , overlayViewFramesize))
label.text = " \nLoading, please wait\n "
label.tag = 12
overlayView.addSubview(label)
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.numberOfLines = 0 //as many as needed


label.sizeToFit()
label.textAlignment = NSTextAlignment.Center

label.center = CGPointMake(overlayViewFramesize / 2, overlayViewFramesize / 2)
overlayView.bringSubviewToFront(label)
window.addSubview(overlayView)

overlayView.layer.addAnimation(spring, forKey: nil)
RunAfterDelay(10.0) {
if self.hidden == true { return }

//strongSelf boilerplate code technique from https://www.raywenderlich.com/133102/swift-style-guide-april-2016-update?utm_source=raywenderlich.com+Weekly&utm_campaign=ea47726fdd-raywenderlich_com_Weekly4_26_2016&utm_medium=email&utm_term=0_83b6edc87f-ea47726fdd-415681129

UIView.animateWithDuration(2, delay: 0, options: [UIViewAnimationOptions.CurveEaseInOut, UIViewAnimationOptions.BeginFromCurrentState, UIViewAnimationOptions.TransitionCrossDissolve], animations: { [weak self] in
guard let strongSelf = self else { return }
(strongSelf.overlayView.viewWithTag(12) as! UILabel).text = randomPhrase()
(strongSelf.overlayView.viewWithTag(12) as! UILabel).sizeToFit()
print ((strongSelf.overlayView.viewWithTag(12) as! UILabel).bounds.width)
(strongSelf.overlayView.viewWithTag(12) as! UILabel).center = CGPointMake(overlayViewFramesize / 2, overlayViewFramesize / 2)

}, completion: { (finished: Bool)in
print ("animation to change label occured")})

}




}
}
func hideOverlayView() {
hidden = true
UIView.animateWithDuration(1.0, delay: 0.0, options: [UIViewAnimationOptions.BeginFromCurrentState], animations: { [unowned self] in
//I know this is clunky... what's the right way?
(self.overlayView.viewWithTag(12) as! UILabel).text = ""
self.overlayView.alpha = 0
}) { [unowned self] _ in
//I know this is clunky. what's the right way?
for view in self.overlayView.subviews {
view.removeFromSuperview()
}

self.overlayView.removeFromSuperview()

print("overlayView after removing:", self.overlayView.description)

}

//here i have to deinitialize stuff to prepare for the next use


}

deinit {
print("Loading Overlay deinit")
}


}

最佳答案

我基本上想要的是能够延迟一段代码,并可能在执行之前取消它。我在这里找到了答案:

GCD and Delayed Invoking

关于ios - 一旦异步调用在 swift 中出现,就停止它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37033940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com