user4234
user4234

Reputation: 1527

Subclassing UIActivityIndicator to change the Image

This is what I did:

@implementation BGUIActivityIndicator

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    [self customInitialize];
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    [self customInitialize];
    return self;
}

-(void)customInitialize
{
    UIView * theImageView= [self findASubViewforClass:[UIImageView class]];//
    UIImageView * theImageView1 = (UIImageView *) theImageView;
    theImageView1.image = [UIImage imageNamed:@"spinner_blue"];
    [theImageView1.image saveScreenshot];
    while (false) ;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

Everything seems perfect. [theImageView1.image saveScreenshot]; jot down both the old and new view perfectly.

However, nothing changes. Why?

I am not exactly asking how to change the image of UIActivityIndicator. There are tons of it already. I want to use it by subclassing UIActivityIndicator because I think it's the most elegant solution. I can't seem to do that.

In particular, I am asking why my approach, which works for changing background of search controller, for example, doesn't work for this.

Upvotes: 0

Views: 695

Answers (1)

Shamsudheen TK
Shamsudheen TK

Reputation: 31339

According to the UIActivityIndicatorView Class Reference ,there is no way/ chance to change the image through sub-classing.

However you can change its activityIndicatorViewStyle , color of the activity indicator,UIActivityIndicatorStyle etc..

I think, without sub-classing, the class class UIImageView provides a very useful and simple way to implement such a thing. The only thing you have to do is to:

1.Provide a number of images that reflect your indicator animation.
2.Create a new UIImageView instance and set images and animation duration.
3.Position your custom activity indicator within your current view.

SAMPLE CODE:

//Create the first status image and the indicator view
UIImage *statusImage = [UIImage imageNamed:@"status1.png"];
UIImageView *activityImageView = [[UIImageView alloc] 
                initWithImage:statusImage];


//Add more images which will be used for the animation
activityImageView.animationImages = [NSArray arrayWithObjects:
            [UIImage imageNamed:@"status1.png"],
            [UIImage imageNamed:@"status2.png"],
            [UIImage imageNamed:@"status3.png"],
            [UIImage imageNamed:@"status4.png"],
            [UIImage imageNamed:@"status5.png"],
            [UIImage imageNamed:@"status6.png"],
            [UIImage imageNamed:@"status7.png"],
            [UIImage imageNamed:@"status8.png"],
            nil];


//Set the duration of the animation (play with it
//until it looks nice for you)
activityImageView.animationDuration = 0.8;


//Position the activity image view somewhere in 
//the middle of your current view
activityImageView.frame = CGRectMake(
            self.view.frame.size.width/2
                -statusImage.size.width/2, 
            self.view.frame.size.height/2
                -statusImage.size.height/2, 
            statusImage.size.width, 
            statusImage.size.height);

//Start the animation
[activityImageView startAnimating];


//Add your custom activity indicator to your current view
[self.view addSubview:activityImageView];

See the full details Here

Upvotes: 2

Related Questions