Skip to content

Instantly share code, notes, and snippets.

@jakejscott
Last active December 12, 2015 08:29
Show Gist options
  • Save jakejscott/4744588 to your computer and use it in GitHub Desktop.
Save jakejscott/4744588 to your computer and use it in GitHub Desktop.
Simple HUD View for iOS
#import "HudView.h"
@implementation HudView {
}
+ (HudView *)hudInView:(UIView *)view animated:(BOOL)animated {
HudView *hudView = [[HudView alloc] initWithFrame:view.bounds];
hudView.opaque = NO;
[view addSubview:hudView];
view.userInteractionEnabled = NO;
[hudView showAnimated:animated];
return hudView;
}
- (void)showAnimated:(BOOL)animated {
if (animated) {
self.alpha = 0.0f;
self.transform = CGAffineTransformMakeScale(1.3f, 1.3f);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
self.alpha = 1.0;
self.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
[UIView commitAnimations];
}
}
- (void)drawRect:(CGRect)rect {
const CGFloat boxHeight = 96.0f;
const CGFloat boxWidth = 96.0f;
// uses the roundf() function to make sure the rectangle doesn’t end up on fractional pixel
// boundaries because that makes the image look fuzzy.
CGRect boxRect = CGRectMake(
roundf(self.bounds.size.width - boxWidth) / 2.0f,
roundf(self.bounds.size.height - boxHeight) / 2.0f,
boxWidth,
boxHeight
);
UIBezierPath *roundRect = [UIBezierPath bezierPathWithRoundedRect:boxRect cornerRadius:10.0f];
[[UIColor colorWithWhite:0.0f alpha:0.75f] setFill];
[roundRect fill];
UIImage *image = [UIImage imageNamed:@"Checkmark"];
CGPoint imagePoint = CGPointMake(
self.center.x - roundf(image.size.width / 2.0f),
self.center.y - roundf(image.size.height / 2.0f) - boxHeight / 8.0f);
[image drawAtPoint:imagePoint];
[[UIColor whiteColor] set];
UIFont *font = [UIFont boldSystemFontOfSize:16.0f];
CGSize textSize = [self.text sizeWithFont:font];
CGPoint textPoint = CGPointMake(
self.center.x - roundf(textSize.width / 2.0f),
self.center.y - roundf(textSize.height / 2.0f) + boxHeight / 4.0f);
[self.text drawAtPoint:textPoint withFont:font];
}
@end
...
- (IBAction)done:(id)sender {
HudView *hudView = [HudView hudInView:self.navigationController.view animated:YES];
hudView.text = @"Tagged";
[self performSelector:@selector(closeScreen) withObject:nil afterDelay:0.6];
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment