Skip to content

Instantly share code, notes, and snippets.

@sehugg
Created December 10, 2013 17:36
Show Gist options
  • Save sehugg/7894677 to your computer and use it in GitHub Desktop.
Save sehugg/7894677 to your computer and use it in GitHub Desktop.
VTUIUtils.h/m - spinner overlay
// VTUIUtils.h
#import <Foundation/Foundation.h>
@interface VTUIUtils : NSObject {
}
+ (void)fadeInOverlay: (UIView*)parentView;
+ (void)fadeOutOverlay: (UIView*)parentView;
+ (void)fadeInOverlayOnMainWindow;
+ (void)fadeOutOverlayOnMainWindow;
+ (void)accessibilityAnnouncement: (NSString*)message;
@end
// VTUIUtils.m
#import "VTUIUtils.h"
#import <QuartzCore/QuartzCore.h>
@implementation VTUIUtils
static NSMutableDictionary* _fadeMap;
+ (void)accessibilityAnnouncement: (NSString*)message
{
// TODO: if (UIAccessibilityIsVoiceOverRunning())
{
UIAccessibilityPostNotification(1008, message);
//UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, msg);
}
}
+ (void)fadeInOverlay: (UIView*)parentView
{
if (parentView == nil)
{
NSLog(@"fadeOutOverlay: No corresponding view (%@)", parentView);
return;
}
if ( [_fadeMap objectForKey:parentView] != nil )
{
NSLog(@"fadeOutOverlay: View exists (%@)", parentView);
return;
}
UIView* fadeView = [[UIView alloc] initWithFrame:parentView.frame];
UIActivityIndicatorView* spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
fadeView.alpha = 0;
if (fadeView != nil)
{
UIView* darkView = [[[UIView alloc] initWithFrame:fadeView.frame] autorelease];
darkView.backgroundColor = [UIColor blackColor];
darkView.alpha = 0.33;
spinner.center = fadeView.center;
CGRect rect = spinner.frame;
rect.origin.x -= rect.size.width/2;
rect.origin.y -= rect.size.height/2;
rect.size.width *= 2;
rect.size.height *= 2;
//UIButton* blackrect = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIView* blackrect = [[[UIView alloc] initWithFrame: rect] autorelease];
blackrect.backgroundColor = [UIColor blackColor];
blackrect.opaque = NO;
blackrect.layer.cornerRadius = 8.0;
[fadeView addSubview:darkView];
[fadeView addSubview:blackrect];
[fadeView addSubview:spinner];
[parentView addSubview:fadeView];
[spinner startAnimating];
[UIView beginAnimations:nil context:fadeView];
[UIView setAnimationDuration: 0.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
fadeView.alpha = 1;
[UIView commitAnimations];
if (_fadeMap == nil)
_fadeMap = [[NSMutableDictionary alloc] initWithCapacity:3];
id key = [NSNumber numberWithInt:(int)parentView];
[_fadeMap setObject:fadeView forKey:key];
}
}
+ (void)fadeOutAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(UIView*)context
{
// context contains the fade out UIView
[context removeFromSuperview];
context = nil;
}
+ (void)fadeOutOverlay: (UIView*)parentView
{
id key = [NSNumber numberWithInt:(int)parentView];
UIView* _fadeView = [_fadeMap objectForKey:key];
if (_fadeView != nil)
{
[UIView beginAnimations:nil context:_fadeView];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration: 0.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:[VTUIUtils class]];
[UIView setAnimationDidStopSelector:@selector(fadeOutAnimationDidStop:finished:context:)];
_fadeView.alpha = 0;
[UIView commitAnimations];
[_fadeMap removeObjectForKey:key];
} else {
NSLog(@"fadeOutOverlay: No corresponding view (%@)", parentView);
}
}
+ (void)fadeInOverlayOnMainWindow
{
[VTUIUtils fadeInOverlay: [[[UIApplication sharedApplication] windows] objectAtIndex:0]];
}
+ (void)fadeOutOverlayOnMainWindow
{
[VTUIUtils fadeOutOverlay: [[[UIApplication sharedApplication] windows] objectAtIndex:0]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment