浅谈 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()
}

以下是代码的输出

645016-20151106113728711-488464828.png

定制 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()
}

645016-20151106113741852-1492139334.png

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

转载于:https://www.cnblogs.com/Ray-liang/p/4942148.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页