开发者

(iphone) make singleton of custom activity indicator?

I'm trying to add my custom activity indicator to my app.

Since there are many places I want to show the indicator,

I thought maybe it's a good idea to make it singleton to access it easily.

I guess I can slightly modify this to make the desired singleton class. Si开发者_StackOverflow社区ngleton Class iPhone

One problem is that I need to add the indicator view to some view(as subview) obviously.

And wonder if there is a singleton -like view that I can access from anywhere to add the indicator view as subview.

Thank you


you can use singleton, but I would advise you not to use singleton for UIView elements.

UIView elements can only have one superview, so if you use singleton.activityindicator everywhere, you will have to remove it from superview before adding it to new view, so alot of bookkeeping is required. For example, you have to remove it from prev superview when showing it else where, and when you come back to prev superview (through user clicking on some nav control or something), you have to determine if you need to now add it back to the new superview, etc.

I do use singleton for one UIView in my design, that is Ad banner view. I wanted to keep one ad across the app, to have same ad in different nav controllers. However it was big pain in the butt.

Just create one, add to the view, and remove when done.. simpler in my opinion :)


You can try to add a UIView to the custom class and then add the activityIndicator to it.


Take a UIView Class

inside initWithFrame methode:

 //this method create custom activity indicator
     UIImage *startImage = [UIImage imageNamed:@"1.png"];
     customActivityIndicator = [[UIImageView alloc] //take in in .h file
            initWithImage:startImage ];


   //Add more images which to be used for the animation
   customActivityIndicator.animationImages = [NSArray arrayWithObjects:
           [UIImage imageNamed:@"1.png"],
           [UIImage imageNamed:@"2.png"],
           [UIImage imageNamed:@"3.png"],
           [UIImage imageNamed:@"4.png"],
           [UIImage imageNamed:@"5.png"],
           [UIImage imageNamed:@"6.png"],
           [UIImage imageNamed:@"7.png"],
           [UIImage imageNamed:@"8.png"],
           nil];


  //Set animation duration 
  customActivityIndicator.animationDuration = 0.5;


  //set frame at the middle of the imageview 
  customActivityIndicator.frame = self.frame;

Take a methode:

// call this methode from the class where u want to animate custom indicator
(void)startAnimating
{
    [customActivityIndicatorstartAnimating];
}

Take a methode:

// call this methode from the class where u want to stope animate custom 
(void)stopAnimating 
{
    [customActivityIndicatorstaopAnimating];
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜