【问题标题】:Activity Indicator Will Not Stop or Hide活动指示器不会停止或隐藏
【发布时间】:2018-08-26 03:44:21
【问题描述】:

我正在为我的 IOS webview 应用程序使用自定义活动指示器 (http://swiftonic.com/customized-activity-indicator-in-swift-3/)。它正在工作,但我遇到的问题是页面加载时指示器不会隐藏或消失。动画只是继续。这是我的代码

class ViewController: UIViewController, UIWebViewDelegate {

@IBOutlet var myWebView: WKWebView!
//@IBOutlet weak var activityIndicator: UIActivityIndicatorView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // In order to   Show activityIndicatorView
    ViewControllerUtils().showActivityIndicator(uiView: self.view)

    // In order to hide activityIndicatorView
    ViewControllerUtils().hideActivityIndicator(uiView: self.view)

    let url = URL(string: "https://oasis.geneseo.edu")!
    myWebView.navigationDelegate = self as? WKNavigationDelegate
    myWebView.load(URLRequest(url: url))

}

根据指示ViewControllerUtils().showActivityIndicator(uiView: self.view) 应该显示指标和ViewControllerUtils().showActivityIndicator(uiView: self.view)。这是我存储UIActivityIndicatorView 的单独文件中的代码。

import Foundation
import UIKit
class ViewControllerUtils {

    var container: UIView = UIView()
    var loadingView: UIView = UIView()
    var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
    /*
     Show customized activity indicator,
     actually add activity indicator to passing view

     @param uiView - add activity indicator to this view
     */
    func showActivityIndicator(uiView: UIView) {
        container.frame = uiView.frame
        container.center = uiView.center
        container.backgroundColor = UIColorFromHex(rgbValue: 0xffffff, alpha: 0.3)

        loadingView.frame = CGRect(x: 0, y: 0, width:  80, height: 80)
        loadingView.center = uiView.center
        loadingView.backgroundColor = UIColorFromHex(rgbValue: 0x444444, alpha: 0.7)
        loadingView.clipsToBounds = true
        loadingView.layer.cornerRadius = 10

        activityIndicator.frame = CGRect(x: 0, y: 0, width:  50, height: 50);
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
        activityIndicator.center = CGPoint(x: loadingView.frame.size.width / 2, y: loadingView.frame.size.height / 2);
        loadingView.addSubview(activityIndicator)
        container.addSubview(loadingView)
        uiView.addSubview(container)
        activityIndicator.startAnimating()
    }
    /*
     Hide activity indicator
     Actually remove activity indicator from its super view

     @param uiView - remove activity indicator from this view
     */
    func hideActivityIndicator(uiView: UIView) {
        activityIndicator.stopAnimating()
        container.removeFromSuperview()
    }
    /*
     Define UIColor from hex value

     @param rgbValue - hex color value
     @param alpha - transparency level
     */
    func UIColorFromHex(rgbValue:UInt32, alpha:Double=1.0)->UIColor {
        let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
        let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
        let blue = CGFloat(rgbValue & 0xFF)/256.0
        return UIColor(red:red, green:green, blue:blue, alpha:CGFloat(alpha))
    }

}

提前致谢。

【问题讨论】:

  • 您将UIActivityIndicatorView 对象存储在哪里?也显示ViewControllerUtils 的代码。\
  • 我可以看到showhide 正文
  • 我将UIActivityIndicatorView 存储在一个单独的文件中。我用ViewControllerUtils 代码更新了我的问题。

标签: ios swift


【解决方案1】:

在您的代码中,您正在创建 2 个不同的 ViewControllerUtils 对象。您正在显示第一个,并隐藏第二个。

你可以这样解决:

let indicator = ViewControllerUtils()
indicator.showActivityIndicator(uiView: self.view)
indicator.hideActivityIndicator(uiView: self.view)

【讨论】:

  • 不幸的是,这不起作用。当我这样做时,指示器根本不显示。
  • 这说明我的回答是正确的。指示器停止并隐藏。您现在遇到了一个新问题:您在下一行代码中显示并立即隐藏它。显示和隐藏之间没有时间间隔,所以看起来就像是立即隐藏。稍后,您需要在 webview 加载后隐藏指示器。您可以为此使用 webview 委托方法之一。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多