Skip to content

Instantly share code, notes, and snippets.

@ks-simakov
Created August 5, 2014 03:37
Show Gist options
  • Save ks-simakov/be8ac3ba0712bc669302 to your computer and use it in GitHub Desktop.
Save ks-simakov/be8ac3ba0712bc669302 to your computer and use it in GitHub Desktop.
Basic view utils (Alets, Notification messages)
//
// UIView+Utilities.h
//
// Created by Konstantin Simakov
//
#import <Foundation/Foundation.h>
@interface UIView (Utilities)
-(void)showActivityIndicator;
-(void)showActivityIndicatorWithAlpha:(CGFloat)alpha andPosition:(UIViewContentMode)contentMode;
-(void)hideActivityIndicator;
-(void)showMessageWithTitle:(NSString *)title andText:(NSString *)text;
-(void)showMessageWithTitle:(NSString *)title andText:(NSString *)text withDelay:(NSTimeInterval)showDelay;
-(void)showErrorWithTitle:(NSString *)title andText:(NSString *)text;
-(void)showErrorWithTitle:(NSString *)title andText:(NSString *)text withDelay:(NSTimeInterval)showDelay;
-(void)showAlertMessageWithTitle:(NSString *)title andText:(NSString *)text;
@end
//
// UIView+Utilities.m
//
// Created by Konstantin Simakov
//
#import "UIView+Utilities.h"
#import <MBProgressHUD.h>
typedef NS_ENUM(NSInteger, FViewMessageType) {
FViewMessageTypeAlert,
FViewMessageTypeError
};
@implementation UIView (Utilities)
UIActivityIndicatorView *_activityIndicator;
UIView *_wrapperView;
MBProgressHUD *_hud;
-(void)showActivityIndicator
{
[self showActivityIndicatorWithAlpha:0.5 andPosition:UIViewContentModeCenter];
}
-(void)hideActivityIndicator
{
if (!_activityIndicator) {
return;
}
[_activityIndicator stopAnimating];
[_activityIndicator removeFromSuperview];
_activityIndicator = nil;
[_wrapperView removeFromSuperview];
}
-(void)showActivityIndicatorWithAlpha:(CGFloat)alpha andPosition:(UIViewContentMode)contentMode
{
if (!_activityIndicator) {
_wrapperView = [[UIView alloc] init];
_wrapperView.backgroundColor = [UIColor colorWithWhite:0 alpha:alpha];
[self addSubview:_wrapperView];
_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[_wrapperView addSubview:_activityIndicator];
}
_wrapperView.frame = self.bounds;
CGRect frame = self.bounds;
CGFloat size = 40;
switch (contentMode) {
case UIViewContentModeCenter:
frame = self.bounds;
break;
case UIViewContentModeTop:
frame = CGRectMake(0, 0, frame.size.width, size);
break;
case UIViewContentModeBottom:
frame = CGRectMake(0, frame.size.height - size, frame.size.width, size);
break;
case UIViewContentModeLeft:
frame = CGRectMake(0, 0, size, frame.size.height);
break;
case UIViewContentModeRight:
frame = CGRectMake(frame.size.width - size, 0, size, frame.size.height);
break;
default:
frame = self.bounds;
break;
}
_activityIndicator.frame = frame;
[_activityIndicator startAnimating];
[self bringSubviewToFront:_wrapperView];
}
-(void)showMessageWithTitle:(NSString *)title andText:(NSString *)text
{
[self showMessageWithTitle:title andText:text withDelay:0.1];
}
-(void)showMessageWithTitle:(NSString *)title andText:(NSString *)text withDelay:(NSTimeInterval)showDelay
{
[self showHudWithType:FViewMessageTypeAlert title:title andText:text withDelay:showDelay];
}
-(void)showErrorWithTitle:(NSString *)title andText:(NSString *)text
{
[self showErrorWithTitle:title andText:text withDelay:0.1];
}
-(void)showErrorWithTitle:(NSString *)title andText:(NSString *)text withDelay:(NSTimeInterval)showDelay
{
[self showHudWithType:FViewMessageTypeError title:title andText:text withDelay:showDelay];
}
-(void)showHudWithType:(FViewMessageType)type title:(NSString *)title andText:(NSString *)text withDelay:(NSTimeInterval)showDelay
{
UIWindow *window = self.window;
if (!window) {
window = [[UIApplication sharedApplication] keyWindow];
}
if (!_hud) {
_hud = [[MBProgressHUD alloc] initWithWindow:window];
[window addSubview:_hud];
}
switch (type) {
case FViewMessageTypeAlert:
_hud.mode = MBProgressHUDModeText;
break;
case FViewMessageTypeError: {
_hud.mode = MBProgressHUDModeCustomView;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alert_error_icon"]];
_hud.customView = imageView;
break;
}
default:
break;
}
_hud.animationType = MBProgressHUDAnimationFade;
_hud.yOffset = 170;
_hud.margin = 8;
_hud.userInteractionEnabled = NO;
_hud.labelText = title;
_hud.detailsLabelText = text;
_hud.labelFont = [UIFont fontWithName:@"Helvetica" size:16];
_hud.detailsLabelFont = [UIFont fontWithName:@"Helvetica-Light" size:14];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, showDelay * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[_hud show:YES];
[_hud hide:YES afterDelay:3];
});
}
-(void)showAlertMessageWithTitle:(NSString *)title andText:(NSString *)text
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:text
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment