3
votes

I have implemented a custom activity indicator view for my iOS apps, which works smooth. The requirement is to set the alpha of superview to 50% (or make it translucent) while the custom activity indicator is animating, and set it to normal when custom activity indicator stops. I am using following class method to add custom activity indicator to view.

+ (CustomActivityIndicatorView*)addCustomActivityIndicatorToView:(UIView *)view
{
    CustomActivityIndicatorView *customActivityIndicatorView = [[CustomActivityIndicatorView alloc]init];
    customActivityIndicatorView.hidesWhenStopsAnimating = NO;
    [customActivityIndicatorView setFrame:CGRectMake(view.bounds.size.width/2-(customActivityIndicatorView.frame.size.width/2), view.bounds.size.height/2-(customActivityIndicatorView.frame.size.height/2), customActivityIndicatorView.frame.size.width, customActivityIndicatorView.frame.size.height)];
    [view addSubview:customActivityIndicatorView];
    [customActivityIndicatorView startAnimating];

    return customActivityIndicatorView;
}

The problem is, when I set [view setAlpha:0.5], it sets alpha to all the subviews including the custom activity indicator. I want to fade the superview not the custom activity indicator.

Read all these links;

https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html

UIView group opacity in single view heirachy

iOS controlling UIView alpha behaviour for subviews

Also tried to set layer.shouldRasterize and the plist key for group opacity but nothing helped. Please help!

2

2 Answers

4
votes

Instead of fading the whole superview, you can drop a semi-white or semi-black overlay on top of it, between your superview and your indicator. That'll do the trick visually.

The overlay would have alpha = 1. and backgroundColor = [UIColor colorWithWhite:1. alpha:.5]

1
votes

Just set the background color of your view to a semi-transparent white:

customActivityIndicatorView.backgroundColor = [UIColor colorWithWhite:1. alpha:.5];