浅谈 iOS 中的 Activity Indicator

Activity Indicator 是iOS开发中必不可少的一个视图。本文就简单地总结一下这个Activity Indicator 的使用方法。

默认 Activity Indicator

以下的函数可以向视图添加一个默认的 Activity Indicator

func showActivityIndicatory(parentView: UIView) {
    
    let activityIndicator = UIActivityIndicatorView()
        
    activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0)
    activityIndicator.center = parentView.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    
    parentView.addSubview(activityIndicator)
    activityIndicator.startAnimating()
}

以下是代码的输出

默认 Activity Indicator

定制 Activity Indicator

以下是将自定义的 Activity indicator 视图

func showActivityIndicatory(parentView: UIView) {
    let container: UIView = UIView()
    container.frame = parentView.frame
    container.center = parentView.center
    container.backgroundColor = UIColorFromHex(0xffffff, alpha: 0.3)
    
    let loadingView: UIView = UIView()
    loadingView.frame = CGRectMake(0, 0, 80, 80)
    loadingView.center = parentView.center
    loadingView.backgroundColor = UIColorFromHex(0x444444, alpha: 0.7)
    loadingView.clipsToBounds = true
    loadingView.layer.cornerRadius = 10
    
    let activityIndicator = UIActivityIndicatorView()
    activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    activityIndicator.activityIndicatorViewStyle =
        UIActivityIndicatorViewStyle.WhiteLarge
    activityIndicator.center = CGPointMake(loadingView.frame.size.width / 2,
        loadingView.frame.size.height / 2);
    loadingView.addSubview(activityIndicator)
    container.addSubview(loadingView)
    parentView.addSubview(container)
    activityIndicator.startAnimating()
}
定制Indicator

在这个函数中我加入了一个透明视图作为Activiate indicator 的容器。

上一篇:L9110 - 电机控制


下一篇:浅谈iOS 自动调节文本高度