微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – Swift中的活动指标

我正在尝试使用 swift制作活动指示器,但有一些我缺少的东西

以下是启动活动按钮按钮的代码

Indicator = ActivityIndicator().StartActivityIndicator(ViewController());

这是停止活动按钮的代码

ActivityIndicator().StopActivityIndicator(ViewController(),indicator: Indicator);

和ACtivity指标类的代码

class ActivityIndicator: NSObject {

    var myActivityIndicator:UIActivityIndicatorView!

    func StartActivityIndicator(obj:UIViewController) -> UIActivityIndicatorView
    {

        self.myActivityIndicator = UIActivityIndicatorView(frame:CGRectMake(100,100,100)) as UIActivityIndicatorView;

        self.myActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        self.myActivityIndicator.center = obj.view.center;

        obj.view.addSubview(myActivityIndicator);

        self.myActivityIndicator.startAnimating();
        return self.myActivityIndicator;
    }

    func StopActivityIndicator(obj:UIViewController,indicator:UIActivityIndicatorView)-> Void
    {
            indicator.removeFromSuperview();
    }


}

Thanks Problem Solved

while am writing code I kNow that time that I have to pass self in the
function but at that time I didn’t kNow What type is self referencing
so I I can’t receive until I kNow the type then I decide to try to
receive different type (UIView,
UIActivityIndicatorView,UIViewController) then I found That it should
be UIViewController but I forgot that it should be pass with self and
I looking for what to pass in function calling and I thought let’s try
UIViewController and it didn’t give me error so I thought it’s right
but when it’s not working I thought I am close enough so it’s better
to ask expert about the problem. And as I expected problem is solved
within few minutes

Swift 3版

class ActivityIndicator: NSObject {

    var myActivityIndicator:UIActivityIndicatorView!

    func StartActivityIndicator(obj:UIViewController) -> UIActivityIndicatorView
    {

        self.myActivityIndicator = UIActivityIndicatorView(frame:CGRect(x: 100,y: 100,width: 100,height: 100)) as UIActivityIndicatorView

        self.myActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        self.myActivityIndicator.center = obj.view.center;

        obj.view.addSubview(myActivityIndicator);

        self.myActivityIndicator.startAnimating();
        return self.myActivityIndicator;
    }

    func StopActivityIndicator(obj:UIViewController,indicator:UIActivityIndicatorView)-> Void
    {
        indicator.removeFromSuperview();
    }


}

解决方法

似乎每次调用start / stop时,都会创建一个ViewController的新实例
只需将ViewController替换为self并尝试

例如

Indicator = ActivityIndicator().StartActivityIndicator(self);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐