Activity indicator with custom image

后端 未结 10 1519
甜味超标
甜味超标 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:48

    You can create your custom activity Indicator with this in Swift 3 & 4:

    Create a new file with name: UIViewExtension.Swift and copy this code and paste in your new file file:

    import UIkit
    
    extension UIView{
       func customActivityIndicator(view: UIView, widthView: CGFloat? = nil,backgroundColor: UIColor? = nil, message: String? = nil,colorMessage:UIColor? = nil ) -> UIView{
    
        //Config UIView
        self.backgroundColor = backgroundColor ?? UIColor.clear
        self.layer.cornerRadius = 10
    
    
        var selfWidth = view.frame.width - 100
        if widthView != nil{
            selfWidth = widthView ?? selfWidth
        }
    
        let selfHeigh = CGFloat(100)
        let selfFrameX = (view.frame.width / 2) - (selfWidth / 2)
        let selfFrameY = (view.frame.height / 2) - (selfHeigh / 2)
        let loopImages = UIImageView()
    
        //ConfigCustomLoading with secuence images
        let imageListArray = [UIImage(named:""),UIImage(named:""), UIImage(named:"")]
        loopImages.animationImages = imageListArray
        loopImages.animationDuration = TimeInterval(1.3)
        loopImages.startAnimating()
        let imageFrameX = (selfWidth / 2) - 17
        let imageFrameY = (selfHeigh / 2) - 35
        var imageWidth = CGFloat(35)
        var imageHeight = CGFloat(35)
    
        if widthView != nil{
            imageWidth = widthView ?? imageWidth
            imageHeight = widthView ?? imageHeight
        }
    
        //ConfigureLabel
        let label = UILabel()
        label.textAlignment = .center
        label.textColor = .gray
        label.font = UIFont.boldSystemFont(ofSize: 17)
        label.numberOfLines = 0
        label.text = message ?? ""
        label.textColor = colorMessage ?? UIColor.clear
    
        //Config frame of label
        let labelFrameX = (selfWidth / 2) - 100
        let labelFrameY = (selfHeigh / 2) - 10
        let labelWidth = CGFloat(200)
        let labelHeight = CGFloat(70)
    
        //add loading and label to customView
        self.addSubview(loopImages)
        self.addSubview(label)
    
        //Define frames
        //UIViewFrame
        self.frame = CGRect(x: selfFrameX, y: selfFrameY, width: selfWidth , height: selfHeigh)
    
        //ImageFrame
        loopImages.frame = CGRect(x: imageFrameX, y: imageFrameY, width: imageWidth, height: imageHeight)
    
        //LabelFrame
        label.frame = CGRect(x: labelFrameX, y: labelFrameY, width: labelWidth, height: labelHeight)
    
        return self
    
    }
    
    }
    

    And then you can use it in your ViewController like this:

    import UIKit
    
    
    class ExampleViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.view.addSubview(UIView().customActivityIndicator(view: self.view,backgroundColor: UIColor.green))
    
        }
    
       //function for stop and desappear loading
       func deseappearLoading(){
          self.view.subviews.last?.removeFromSuperview()
       }
    }
    

    Don't forget replace [UIImage(named:" "),UIImage(named:" "), UIImage(named:" ")] with your names of images and adjust the TimeInterval(1.3). Enjoy it.

提交回复
热议问题