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();
}
}
A view that shows that a task is in progress.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With