Activity indicator with custom image

后端 未结 10 1403
甜味超标
甜味超标 2021-01-31 05:58

I am loading a UIWebView and in the meantime I wan\'t to show a blank page with this\"spinner\" activity indic

10条回答
  •  感情败类
    2021-01-31 06:31

    SWIFT 4 Sweet And Simply just put extension UIView{}

    Modified answer of @gandhi Mena

    if you want to create your own custom Loading indicator

    Create a UIView extension which create and customize your brand logo as a custom indicator put this code in you global declaration file.

    extension UIView{
    func customActivityIndicator(view: UIView, widthView: CGFloat?,backgroundColor: UIColor?, textColor:UIColor?, message: String?) -> UIView{
    
        //Config UIView
        self.backgroundColor = backgroundColor //Background color of your view which you want to set
    
        var selfWidth = view.frame.width
        if widthView != nil{
            selfWidth = widthView ?? selfWidth
        }
    
        let selfHeigh = view.frame.height
        let loopImages = UIImageView()
    
        let imageListArray = ["image1", "image2"] // Put your desired array of images in a specific order the way you want to display animation.
    
        loopImages.animationImages = imageListArray
        loopImages.animationDuration = TimeInterval(0.8)
        loopImages.startAnimating()
    
        let imageFrameX = (selfWidth / 2) - 30
        let imageFrameY = (selfHeigh / 2) - 60
        var imageWidth = CGFloat(60)
        var imageHeight = CGFloat(60)
    
        if widthView != nil{
            imageWidth = widthView ?? imageWidth
            imageHeight = widthView ?? imageHeight
        }
    
        //ConfigureLabel
        let label = UILabel()
        label.textAlignment = .center
        label.textColor = .gray
        label.font = UIFont(name: "SFUIDisplay-Regular", size: 17.0)! // Your Desired UIFont Style and Size
        label.numberOfLines = 0
        label.text = message ?? ""
        label.textColor = textColor ?? UIColor.clear
    
        //Config frame of label
        let labelFrameX = (selfWidth / 2) - 100
        let labelFrameY = (selfHeigh / 2) - 10
        let labelWidth = CGFloat(200)
        let labelHeight = CGFloat(70)
    
        // Define UIView frame
        self.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width , height: UIScreen.main.bounds.size.height)
    
    
        //ImageFrame
        loopImages.frame = CGRect(x: imageFrameX, y: imageFrameY, width: imageWidth, height: imageHeight)
    
        //LabelFrame
        label.frame = CGRect(x: labelFrameX, y: labelFrameY, width: labelWidth, height: labelHeight)
    
        //add loading and label to customView
        self.addSubview(loopImages)
        self.addSubview(label)
        return self }}
    

    Hide an indicator something like this you can remove subview at the top from the subview stack. put this code in the same globally declared swift file.

    func hideLoader(removeFrom : UIView){
    removeFrom.subviews.last?.removeFromSuperview()
    }
    

    Now you can shoot at the mark by this code To display activity indicator in your view controller put this code when you want to display.

     self.view.addSubview(UIView().customActivityIndicator(view: self.view, widthView: nil, backgroundColor:"Desired color", textColor: "Desired color", message: "Loading something"))
    

    To hide animating loader you can user above function you defined in the globally. In your ViewController.swift where you want to hide put this line of code.

    hideLoader(removeFrom: self.view)
    

    imageListArray looks like this.

提交回复
热议问题