Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity Indicator in Swift

I am trying to make activity indicator using swift but there is something I am missing

Here is the code for start activity button button:

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

And here is the code for stop activity button

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

and code for ACtivity indicator class is

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 version

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();
    }


}
like image 215
Varun Naharia Avatar asked May 04 '15 01:05

Varun Naharia


People also ask

What is UIActivityIndicatorView?

A view that shows that a task is in progress.

How do I hide activity indicator in Swift?

Select the Activity Indicator in Storyboard, then select property "Hides when stopped". This way you don't have to hide it, just start or stop the animation, and the Activity Indicator will show and hide automatically.


1 Answers

It seem that every time you call start/stop,you create a new instance of ViewController Just replace ViewController to self and try

For example

Indicator = ActivityIndicator().StartActivityIndicator(self);
like image 90
Leo Avatar answered Sep 19 '22 03:09

Leo