Activity Swift 中的指标

Activity Indicator in Swift

我正在尝试使用 swift 制作 activity 指标,但我缺少一些东西

这是启动 activity 按钮按钮的代码:

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

这里是停止 activity 按钮的代码

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

ACtivity 指标 class 的代码是

class ActivityIndicator: NSObject {

    var myActivityIndicator:UIActivityIndicatorView!

    func StartActivityIndicator(obj:UIViewController) -> UIActivityIndicatorView
    {

        self.myActivityIndicator = UIActivityIndicatorView(frame:CGRectMake(100, 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);