gpt4 book ai didi

ios - 从浏览器加载回 iOS 应用程序时,进度指示器无法停止动画

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

在我的应用程序中,我有一个打开浏览器(Safari)的链接,当从浏览器单击返回应用程序时,UIActivityIndi​​catorView无法停止加载或动画,尽管我实现了它来停止动画..任何帮助...这是我的代码

func webViewDidStartLoad(_ : UIWebView){

let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .Alert)

alert.view.tintColor = UIColor.blackColor()
let progressIndicator = UIActivityIndicatorView()

alert.view.addSubview(progressIndicator)

let views = ["alert" : alert.view, "progressIndicator" : progressIndicator]
var constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:[progressIndicator]-(-50)-|", options: [] , metrics: nil, views: views)
constraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|[progressIndicator]|", options: [], metrics: nil, views: views)
alert.view.addConstraints(constraints)

progressIndicator.userInteractionEnabled = false
progressIndicator.startAnimating()

self.presentViewController(alert, animated: true, completion:nil)
}


func webViewDidFinishLoad(_ : UIWebView){
dismissViewControllerAnimated(false, completion: nil)
progressIndicator.stopAnimating()
}

最佳答案

警报 Controller 无法关闭,因为您向 View Controller 呈现了动画,并且在动画完成之前调用了 Web View 委托(delegate)方法 func webViewDidFinishLoad(_ : UIWebView)。

问题有2个解决方案

1) 显示没有动画的警报 View Controller

self.presentViewController(alert, 动画: false, 完成:nil)

2)延迟几毫秒后关闭警报 Controller

letdelayTime=dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))dispatch_after(delayTime,dispatch_get_main_queue()){self.alert.dismissViewControllerAnimated(false,completion:nil)self.progressIndicator.stopAnimating()}

代码片段

let ProgressIndicator = UIActivityIndi​​catorView() letalert = UIAlertController(title: nil, message: "请稍候...", PreferredStyle: .Alert)

func webViewDidStartLoad(_ : UIWebView) {

alert.view.tintColor = UIColor.blackColor()
alert.view.addSubview(progressIndicator)

let views = ["alert" : alert.view, "progressIndicator" : progressIndicator]
var constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:[progressIndicator]-(-50)-|", options: [] , metrics: nil, views: views)
constraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|[progressIndicator]|", options: [], metrics: nil, views: views)
alert.view.addConstraints(constraints)

progressIndicator.userInteractionEnabled = true
progressIndicator.startAnimating()

// If false then 1 solution will work
self.presentViewController(alert, animated: false, completion:nil)
// If true then 2 solution will work
self.presentViewController(alert, animated: true, completion:nil)
}

func webViewDidFinishLoad(_ : UIWebView) {

// 1 Solution
/*self.alert.dismissViewControllerAnimated(false, completion: nil)
self.progressIndicator.stopAnimating()*/

// 2 Solution
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self.alert.dismissViewControllerAnimated(false, completion: nil)
self.progressIndicator.stopAnimating()
}
}

关于ios - 从浏览器加载回 iOS 应用程序时,进度指示器无法停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39447738/

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