How to display an activity indicator with text on iOS 8 with Swift?

前端 未结 14 2161
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 08:58

I wanna show, programmatically, an activity indicator with text, like the one in the Photos app (after editing and saving a picture). How can I do this?

14条回答
  •  独厮守ぢ
    2020-11-27 09:44

    In Swift 3

    Declare variables which we will use

    var activityIndicator = UIActivityIndicatorView()
    let loadingView = UIView()
    let loadingLabel = UILabel()
    

    Set label , view and activityIndicator

    func setLoadingScreen(myMsg : String) {
        let width: CGFloat = 120
        let height: CGFloat = 30
        let x = (self.view.frame.width / 2) - (width / 2)
        let y = (169 / 2) - (height / 2) + 60
        loadingView.frame = CGRect(x: x, y: y, width: width, height: height)
        self.loadingLabel.textColor = UIColor.white
        self.loadingLabel.textAlignment = NSTextAlignment.center
        self.loadingLabel.text = myMsg
        self.loadingLabel.frame = CGRect(x: 0, y: 0, width: 160, height: 30)
        self.loadingLabel.isHidden = false
        self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white
        self.activityIndicator.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        self.activityIndicator.startAnimating()
        loadingView.addSubview(self.spinner)
        loadingView.addSubview(self.loadingLabel)
        self.view.addSubview(loadingView)
    }
    

    Start Animation

    @IBAction func start_animation(_ sender: Any) {
        setLoadingScreen(myMsg: "Loading...")
    }
    

    Stop Animation

    @IBAction func stop_animation(_ sender: Any) {
        self.spinner.stopAnimating()
        UIApplication.shared.endIgnoringInteractionEvents()
        self.loadingLabel.isHidden = true
    }
    

提交回复
热议问题