Swift extension example

后端 未结 8 2140
既然无缘
既然无缘 2020-11-28 01:33

I was originally wanting to know how to make something like this

UIColor.myCustomGreen

so that I could define my own colors and use them th

8条回答
  •  余生分开走
    2020-11-28 02:07

    One of the best example of extension and convenience initializer :

     extension UIActivityIndicatorView {
        convenience init(activityIndicatorStyle: UIActivityIndicatorViewStyle, color: UIColor, placeInTheCenterOf parentView: UIView) {
        self.init(activityIndicatorStyle: activityIndicatorStyle)
        center = parentView.center
        self.color = color
        parentView.addSubview(self)
      }
    }
    

    You can use it in following ways :

    1. Initialize activityIndicator

      let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge, color: .gray,  placeInTheCenterOf: view)
      
    2. Start animating activity indicator

      activityIndicator.startAnimating()
      
    3. Stop animating activity indicator

      activityIndicator.stopAnimating()
      

提交回复
热议问题