도래울

IOS customized activity indicator with Swift 본문

개발/iOS

IOS customized activity indicator with Swift

도래울 2016. 8. 5. 11:01

Default activity indicator

  • Following function will add default activity indicator to a view.
func showActivityIndicatory(uiView: UIView) {
    var actInd: UIActivityIndicatorView = UIActivityIndicatorView()
    actInd.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    actInd.center = uiView.center
    actInd.hidesWhenStopped = true
    actInd.activityIndicatorViewStyle =
                    UIActivityIndicatorViewStyle.WhiteLarge
    uiView.addSubview(actInd)
    activityIndicator.startAnimating()
}
  • Output will be looks like below

Picture

Customized activity indicator

  • Following function add customized activity indicator to a view
func showActivityIndicatory(uiView: UIView) {
    var container: UIView = UIView()
    container.frame = uiView.frame
    container.center = uiView.center
    container.backgroundColor = UIColorFromHex(0xffffff, alpha: 0.3)

    var loadingView: UIView = UIView()
    loadingView.frame = CGRectMake(0, 0, 80, 80)
    loadingView.center = uiView.center
    loadingView.backgroundColor = UIColorFromHex(0x444444, alpha: 0.7)
    loadingView.clipsToBounds = true
    loadingView.layer.cornerRadius = 10

    var actInd: UIActivityIndicatorView = UIActivityIndicatorView()
    actInd.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    actInd.activityIndicatorViewStyle =
                UIActivityIndicatorViewStyle.WhiteLarge
    actInd.center = CGPointMake(loadingView.frame.size.width / 2,
                loadingView.frame.size.height / 2);
    loadingView.addSubview(actInd)
    container.addSubview(loadingView)
    uiView.addSubview(container)
    actInd.startAnimating()
}
  • In this function I have added partially transparent overlay to view and display activity indicator in rounded rectangle

  • Following is the output

Picture

  • Complete source code

https://github.com/erangaeb/dev-notes/blob/master/swift/ViewControllerUtils.swift


'개발 > iOS' 카테고리의 다른 글

UITableView - scroll to the top  (0) 2016.08.09
Comparing NSIndexPath Swift  (0) 2016.08.08
disable ios8 emoticon keyboard  (0) 2016.08.03
change the color of hud place of gray color  (0) 2016.08.01
ios 메모리 에러 찾기 NSZombieEnabled 추가  (0) 2016.07.27
Comments