3

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!

Community
  • 1
  • 1
user3404693
  • 555
  • 1
  • 8
  • 19

2 Answers2

4

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]

Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • Yes, tried to do so and it works, but it disables the user interaction... where as the requirement states that the user could interact with the view while customActivityIndicatorView is spinning. Thanks – user3404693 Jun 10 '14 at 06:59
  • The overlay doesn't need to block user interaction. Just set its `userInteractionEnabled` property to NO. – Cyrille Jun 10 '14 at 07:01
1

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

customActivityIndicatorView.backgroundColor = [UIColor colorWithWhite:1. alpha:.5];
Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • Thanks, but I want the superview (on which the customActivityIndicatorView is added as subview) translucent, not the customActivityIndicatorView's background. – user3404693 Jun 10 '14 at 06:36