Permalink
Browse files

First commit. A HUD for iOS.

  • Loading branch information...
0 parents commit 8c600974d44d5100b2169f916fb031895b97bc48 @y0n3l committed Sep 13, 2011
@@ -0,0 +1,63 @@
+//
+// LGViewHUD.h
+// Created by y0n3l on 4/13/11.
+//
+
+#import <UIKit/UIKit.h>
+
+typedef enum {
+ HUDAnimationNone,
+ HUDAnimationShowZoom,
+ HUDAnimationHideZoom,
+ HUDAnimationHideFadeOut
+} HUDAnimation;
+
+/**
+ A HUD that mimics the native one used in iOS (when you press volume up or down
+ on the iPhone for instance) and also provides some more features (some more animations
+ + activity indicator support included.)
+ */
+
+@interface LGViewHUD : UIView {
+ UIImage* image;
+ UIImageView* imageView;
+ UILabel* bottomLabel;
+ UILabel* topLabel;
+ UIView* backgroundView;
+ NSTimeInterval displayDuration;
+ NSTimer* displayTimer;
+ BOOL activityIndicatorOn;
+ UIActivityIndicatorView* activityIndicator;
+}
+/** The image displayed at the center of the HUD. Default is nil. */
+@property (readwrite, retain) UIImage* image;
+/** The top text of the HUD. Shortcut to the text of the topLabel property. */
+@property (readwrite, retain) NSString* topText;
+/** The bottom text of the HUD. Shortcut to the text of the bottomLabel property. */
+@property (readwrite, retain) NSString* bottomText;
+/** The top label of the HUD. (So that you can adjust its properties ...) */
+@property (readonly) UILabel* topLabel;
+/** The bottom label of the HUD. (So that you can adjust its properties ...) */
+@property (readonly) UILabel* bottomLabel;
+/** HUD display duration. Default is 2 sec. */
+@property (readwrite) NSTimeInterval displayDuration;
+/** Diplays a large white activity indicator instead of the image if set to YES.
+ Default is NO. */
+@property (readwrite) BOOL activityIndicatorOn;
+
+/** Returns the default HUD singleton instance. */
++(LGViewHUD*) defaultHUD;
+
+/** Shows the HUD and hides it after a delay equals to the displayDuration property value.
+ HUDAnimationNone is used by default. */
+-(void) showInView:(UIView*)view;
+
+/** Shows the HUD with the given animation and hides it after a delay equals to the displayDuration property value. */
+-(void) showInView:(UIView *)view withAnimation:(HUDAnimation)animation;
+
+/** Hides the HUD right now.
+ You only need to invoke this one when the HUD is displayed with an activity indicator
+ because there's no auto hide in that case. */
+-(void) hideWithAnimation:(HUDAnimation)animation;
+
+@end
@@ -0,0 +1,236 @@
+//
+// LGViewHUD.m
+// Created by y0n3l on 4/13/11.
+//
+
+#import "LGViewHUD.h"
+#import <QuartzCore/QuartzCore.h>
+
+static LGViewHUD* defaultHUD = nil;
+
+@interface LGViewHUD (privateAPI)
+-(void) startTimerForAutoHide;
+-(void) hideAfterDelay:(NSTimeInterval)delayInSecs withAnimation:(HUDAnimation)animation;
+@end
+
+@implementation LGViewHUD
+
+@synthesize displayDuration;
+@synthesize topLabel, bottomLabel;
+
+#define kHUDDefaultAlphaValue 0.65
+#define kHUDDefaultDisplayDuration 2
+
+- (id)initWithFrame:(CGRect)frame {
+
+ self = [super initWithFrame:frame];
+ if (self) {
+ // Initialization code.
+ double offset = frame.size.height/4.0;
+ topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, offset/3.0, frame.size.width, offset/3.0)];
+ topLabel.backgroundColor=[UIColor clearColor];
+ topLabel.textColor=[UIColor whiteColor];
+ topLabel.font=[UIFont boldSystemFontOfSize:17];
+ topLabel.shadowColor=[UIColor blackColor];
+ topLabel.shadowOffset=CGSizeMake(1, 1);
+ topLabel.textAlignment=UITextAlignmentCenter;
+
+ bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height-2*offset/3.0, frame.size.width, offset/3.0)];
+ bottomLabel.backgroundColor=[UIColor clearColor];
+ bottomLabel.textColor=[UIColor whiteColor];
+ bottomLabel.font=[UIFont boldSystemFontOfSize:17];
+ bottomLabel.shadowColor=[UIColor blackColor];
+ bottomLabel.shadowOffset=CGSizeMake(1, 1);
+
+ bottomLabel.textAlignment=UITextAlignmentCenter;
+ bottomLabel.text=@"test";
+ image=nil;
+
+ backgroundView= [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
+ backgroundView.layer.cornerRadius=10;
+ backgroundView.backgroundColor=[UIColor blackColor];
+ backgroundView.alpha=kHUDDefaultAlphaValue;
+
+ offset=frame.size.width/3.0;
+ imageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width/3.0, frame.size.height/3.0,
+ frame.size.width/3.0, frame.size.height/3.0)];
+ imageView.contentMode=UIViewContentModeScaleAspectFit;// UIViewContentModeCenter;
+ if ([imageView.layer respondsToSelector:@selector(setShadowColor:)]) {
+ imageView.layer.shadowColor=[[UIColor blackColor] CGColor];
+ imageView.layer.shadowOffset = CGSizeMake(1, 1);
+ imageView.layer.shadowOpacity=1.0;
+ imageView.layer.shadowRadius=0.0;
+ }
+ activityIndicator=nil;
+ [self addSubview:backgroundView];
+ [self addSubview:imageView];
+ [self addSubview:topLabel];
+ [self addSubview:bottomLabel];
+ self.userInteractionEnabled=NO;
+ displayDuration=kHUDDefaultDisplayDuration;
+
+ }
+ return self;
+}
+
+- (void)dealloc {
+ [backgroundView release];
+ [topLabel release];
+ [bottomLabel release];
+ [imageView release];
+ backgroundView=nil;
+ topLabel=nil;
+ bottomLabel=nil;
+ imageView=nil;
+ [super dealloc];
+}
+
++(LGViewHUD*) defaultHUD {
+ if (defaultHUD==nil)
+ defaultHUD=[[LGViewHUD alloc] initWithFrame:CGRectMake(0, 0, 160, 160)];
+ return defaultHUD;
+}
+
+-(void) setTopText:(NSString *)t {
+ topLabel.text=t;
+}
+
+-(NSString*) topText {
+ return topLabel.text;
+}
+
+-(void) setBottomText:(NSString *)t {
+ bottomLabel.text=t;
+}
+
+-(NSString*) bottomText {
+ return bottomLabel.text;
+}
+
+/** this disables the activity indicator on if any. */
+-(void) setImage:(UIImage*) img {
+ imageView.image=img;
+ if (activityIndicatorOn)
+ self.activityIndicatorOn=NO;
+}
+
+-(UIImage*) image {
+ return imageView.image;
+}
+
+-(BOOL) activityIndicatorOn {
+ return activityIndicatorOn;
+}
+
+-(void) setActivityIndicatorOn:(BOOL)isOn {
+ if (activityIndicatorOn!=isOn) {
+ activityIndicatorOn=isOn;
+ if (activityIndicatorOn) {
+ activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
+ [activityIndicator startAnimating];
+ activityIndicator.center=CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0);
+ //CGRect frame = CGRectMake(self.frame.size.width/3.0, self.frame.size.height/3.0, self.frame.size.width/3.0, self.frame.size.height/3.0);
+ //activityIndicator.frame=frame;
+ imageView.hidden=YES;
+ [self addSubview:activityIndicator];
+ } else {
+ //when applying an image, this will auto hide the HUD.
+ [activityIndicator removeFromSuperview];
+ imageView.hidden=NO;
+ [activityIndicator release];
+ activityIndicator=nil;
+ }
+ }
+}
+
+-(void) layoutSubviews {
+ [super layoutSubviews];
+}
+
+-(void) showInView:(UIView*)view {
+ [self showInView:view withAnimation:HUDAnimationNone];
+}
+
+-(void) showInView:(UIView *)view withAnimation:(HUDAnimation)animation {
+ switch (animation) {
+ case HUDAnimationNone:
+ self.alpha=1.0;
+ self.center=CGPointMake(view.center.x, view.center.y+18);
+ [view addSubview:self];
+ break;
+ case HUDAnimationShowZoom:
+ self.center=CGPointMake(view.center.x, view.center.y+18);
+ //self.center=CGPointMake(view.frame.size.width/2, view.frame.size.height/2);
+ self.alpha=0;
+ self.transform=CGAffineTransformMakeScale(1.7, 1.7);
+ [view addSubview:self];
+ [UIView beginAnimations:@"HUDShowZoom" context:nil];
+ [UIView setAnimationDuration:0.3];
+ [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
+ self.transform=CGAffineTransformMakeScale(1, 1);
+ self.alpha=1.0;
+ [UIView commitAnimations];
+ default:
+ break;
+ }
+ if (!activityIndicatorOn) {
+ HUDAnimation disappearAnimation = HUDAnimationHideFadeOut;
+ switch (animation) {
+ case HUDAnimationShowZoom:
+ disappearAnimation = HUDAnimationHideZoom;
+ break;
+ default:
+ disappearAnimation = HUDAnimationHideFadeOut;
+ break;
+ }
+ [self hideAfterDelay:displayDuration withAnimation:disappearAnimation ];
+ }
+}
+
+-(void) hideAfterDelay:(NSTimeInterval)delayDuration withAnimation:(HUDAnimation) animation{
+ [displayTimer invalidate];
+ [displayTimer release];
+ displayTimer = [[NSTimer scheduledTimerWithTimeInterval:delayDuration target:self
+ selector:@selector(displayTimeOut:)
+ userInfo:[NSNumber numberWithInt:animation] repeats:NO] retain];
+}
+
+-(void) displayTimeOut:(NSTimer*)timer {
+ [displayTimer release];
+ displayTimer=nil;
+ [self hideWithAnimation:(HUDAnimation)[[timer userInfo] intValue]];
+}
+
+-(void) hideWithAnimation:(HUDAnimation)animation {
+ switch (animation) {
+ case HUDAnimationHideZoom:
+ [UIView beginAnimations:@"HUDHideZoom" context:nil];
+ [UIView setAnimationDuration:0.4];
+ [UIView setAnimationDelegate:self];
+ [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
+ self.transform=CGAffineTransformMakeScale(0.1, 0.1);
+ self.alpha=0;
+ [UIView commitAnimations];
+ break;
+ case HUDAnimationHideFadeOut:
+ [UIView beginAnimations:@"HUDHideFade" context:nil];
+ [UIView setAnimationDelegate:self];
+ [UIView setAnimationDuration:1.0];
+ self.alpha=0.0;
+ [UIView commitAnimations];
+ break;
+ case HUDAnimationNone:
+ default:
+ [self removeFromSuperview];
+ break;
+ }
+}
+
+-(void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
+ if (self.alpha==0.0)
+ [self removeFromSuperview];
+}
+
+
+
+@end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8c60097

Please sign in to comment.