Skip to main content

create a custom alert box with multiple layout options in objective c



//

//  EGviewAlert.h--------------------------


#import <UIKit/UIKit.h>

@interface EGviewAlert : UIView
//alert view with heading message and ok button
typedef void (^closeAction)(id);
typedef void (^cancelAction)(id);
-(void)Alert:(NSString*)heading withMsg:(NSString*)message withOkBtn:(NSString*)okBtnMsg closeAction:(void (^)(id))completeBlock;
//alert view with heading message and ok button and cancel button
-(void)Alert:(NSString*)heading withMsg:(NSString*)message withOkBtn:(NSString*)okBtnMsg withCancelBtn:(NSString*)cancelBtnMsg closeAction:(void (^)(id))completeBlock cancelAction:(void (^)(id))cancelAction;

//for custom layout
-(void)customAlert:(NSDictionary *)messages  closeAction:(void (^)(id))completeBlock cancelAction:(void (^)(id))cancelAction;

//heading view
@property (nonatomic,strong) UIView *HeadingView;
//heading text
@property (nonatomic,strong) UILabel *headingText;
//heading message
@property (nonatomic,strong) UITextView *msgText;
@property (nonatomic,strong) UILabel *subHeadingText;
@property (nonatomic,strong) UITextField *answerTextField;
//clsoe btn
@property (nonatomic,strong) UIButton *closeBtn;
@property (nonatomic,strong) UIButton *cancelBtn;
@property (copy, nonatomic) closeAction CloseAction;
@property (copy, nonatomic) cancelAction CancelAction;
@end



//


//  EGviewAlert.m-----------------------------------------------------


#import "EGviewAlert.h"
#define _width 480.0f
#define _height 320.0f

@implementation EGviewAlert
{
    CGSize  updateSize;
    CGPoint updatedFrame;
    CGFloat headingFont;
    CGFloat msgFont;
    CGFloat space;
    CGFloat button_width;
    CGFloat button_height;
    id data;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        space=20;
        headingFont=21;
        msgFont=17;
        button_width=125;
        button_height=50;
        self.frame=CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
        [self addHeadingView:CGSizeMake(frame.size.width, frame.size.height/5)];
    }
    return self;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        headingFont=21;
        msgFont=17;
          space=20;
           self.frame=CGRectMake(([UIScreen mainScreen].bounds.size.width/2-_width/2), ([UIScreen mainScreen].bounds.size.height/2-_height/2), _width, _height);
         [self addHeadingView:CGSizeMake(self.frame.size.width, self.frame.size.height/5)];
    }
    return self;
}

//@add heading view with size
-(void)addHeadingView:(CGSize)size
{
    _HeadingView=[[UIView alloc] init];
    _HeadingView.frame=CGRectMake(0, 0, size.width, size.height);
    _HeadingView.backgroundColor=[UIColor grayColor];
    [self addSubview:_HeadingView];
    //add uilabel to heading text
    _headingText=[[UILabel alloc] init];
    _headingText.frame=CGRectMake(0, 0, size.width, size.height);
    [_headingText setFont:[UIFont boldSystemFontOfSize:headingFont]];
    _headingText.textAlignment=NSTextAlignmentCenter;
    _headingText.text=@"";
    [_HeadingView addSubview:_headingText];
    ///add msgText
    _msgText=[[UITextView alloc] init];
    _msgText.frame=CGRectMake(0, ((_HeadingView.frame.origin.y+_headingText.frame.size.height)+space), size.width, _height/2);
    _msgText.backgroundColor=[UIColor clearColor];
    [_msgText setFont:[UIFont boldSystemFontOfSize:headingFont]];
    _msgText.textAlignment=NSTextAlignmentCenter;\
    _msgText.editable=false;
    _msgText.scrollEnabled=false;
    _msgText.text=@"";
    [self addSubview:_msgText];
    //add close btn

    
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:self.bounds];
    self.layer.masksToBounds = NO;
    self.layer.cornerRadius=10;
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
    self.layer.shadowOpacity = 0.5f;
    self.layer.shadowPath = shadowPath.CGPath;
}
-(void)ClickEvent:(id)object
{
    self.CloseAction(data);
    [self removeFromSuperview];
}
-(void)CancelEvent:(id)object
{
    self.CancelAction(nil);
    [self removeFromSuperview];
}
//add shadow
-(void)addShadow
{
    
}

//update layout
-(void)updateLayout
{
    
}
//alert with ok btn
-(void)Alert:(NSString *)heading withMsg:(NSString *)message withOkBtn:(NSString *)okBtnMsg closeAction:(void (^)(id))completeBlock
{
    _headingText.text=heading;
    _msgText.text=message;
    
    _closeBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    _closeBtn.frame=CGRectMake((_width/2-button_width/2), _height-75, button_width, button_height);
    _closeBtn.backgroundColor=[UIColor grayColor];
    [_closeBtn setTitle:okBtnMsg forState:UIControlStateNormal];
    [self addSubview:_closeBtn];

    [_closeBtn addTarget:self action:@selector(ClickEvent:) forControlEvents:UIControlEventTouchUpInside];
    self.CloseAction = completeBlock;
}
//alert with ok and cancel button
-(void)Alert:(NSString *)heading withMsg:(NSString *)message withOkBtn:(NSString *)okBtnMsg withCancelBtn:(NSString *)cancelBtnMsg closeAction:(void (^)(id))completeBlock cancelAction:(void (^)(id))cancelAction
{
    _closeBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    _closeBtn.frame=CGRectMake(50, _height-75, button_width, button_height);
    _closeBtn.backgroundColor=[UIColor grayColor];
    [_closeBtn setTitle:okBtnMsg forState:UIControlStateNormal];
    [self addSubview:_closeBtn];
    [_closeBtn addTarget:self action:@selector(ClickEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    _headingText.text=heading;
    _msgText.text=message;
    
    _cancelBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    _cancelBtn.frame=CGRectMake(480-175, _height-75, button_width, button_height);
    _cancelBtn.backgroundColor=[UIColor grayColor];
    [_cancelBtn setTitle:cancelBtnMsg forState:UIControlStateNormal];
    [self addSubview:_cancelBtn];
    [_cancelBtn addTarget:self action:@selector(CancelEvent:) forControlEvents:UIControlEventTouchUpInside];
      self.CloseAction = completeBlock;
    self.CancelAction =cancelAction;
}
//custom alert add any object label textfield button segment etc
-(void)customAlert:(id)messages closeAction:(void (^)(id))completeBlock cancelAction:(void (^)(id))cancelAction
{
    //heading msg
    //other msgs
    _headingText.text=[messages objectForKey:@"heading"];
    _msgText.hidden=true;
    _closeBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    _closeBtn.frame=CGRectMake(50, _height-75, button_width, button_height);
    _closeBtn.backgroundColor=[UIColor grayColor];
    [_closeBtn setTitle:[messages objectForKey:@"closeText"] forState:UIControlStateNormal];
    [self addSubview:_closeBtn];
    [_closeBtn addTarget:self action:@selector(ClickEvent:) forControlEvents:UIControlEventTouchUpInside];
    

    //sub heading text
    _subHeadingText=[[UILabel alloc] init];
    _subHeadingText.frame=CGRectMake(0, 100, self.frame.size.width, 50);
    [_subHeadingText setFont:[UIFont boldSystemFontOfSize:headingFont]];
    _subHeadingText.textAlignment=NSTextAlignmentCenter;
    _subHeadingText.text=[messages objectForKey:@"msg"];
    [self addSubview:_subHeadingText];
    //adding answer text filed
    _answerTextField=[[UITextField alloc] init];
    _answerTextField.textAlignment=NSTextAlignmentCenter;
    _answerTextField.keyboardType=UIKeyboardTypeASCIICapableNumberPad;
    _answerTextField.frame=CGRectMake(25, 150, 400, 50);
    _answerTextField.borderStyle=UITextBorderStyleLine;
    [self addSubview:_answerTextField];
    
    
    
    _cancelBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    _cancelBtn.frame=CGRectMake(480-175, _height-75, button_width, button_height);
    _cancelBtn.backgroundColor=[UIColor grayColor];
    [_cancelBtn setTitle:[messages objectForKey:@"cancelText"]  forState:UIControlStateNormal];
    [self addSubview:_cancelBtn];
    [_cancelBtn addTarget:self action:@selector(CancelEvent:) forControlEvents:UIControlEventTouchUpInside];
    self.CloseAction = completeBlock;
    self.CancelAction =cancelAction;

}
-(void)dealloc
{
    NSLog(@"all relased");
}
@end


importing and added file to your view controller---------------------------------------------------------------------


#import "ViewController.h"
#import "EGviewAlert.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    EGviewAlert *alert=[[EGviewAlert alloc] init];
    alert.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:alert];
    [alert customAlert:nil closeAction:^(id data) {
        //
    } cancelAction:^(id data) {
        //
    }]; // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

Comments

Popular posts from this blog

Better Memory management with PixiJS or How to manage cpu and cpu memory in PixiJS.

PixiJS is my favorite framework when i am looking for a web games specially for mobile or desktop  PixiJS is fast blazing fast and you can get a decent FPS even on older device.   so here is my optimization techniques for PixiJs 1. manage your sprites in a better way use spritesheet to reduce the draw calls create big sprite sheet which contain multiple sprites can be draw in gpu with a single draw call. use TexturePacker  https://www.codeandweb.com/texturepacker  best tool when its comes to spritesheet 2. for floating point calculation round off calculation for example let  speed = 0.75 ; let  position = 100 ; console . log ( Math . round ( speed * position )) 3. don't create very big canvas when u need a big canvas size game just try to create a small canvas and translate it. 4. its very important one managing TextureCache in memory you can get all TextureCache list by using  Object.entries(PIXI.utils.TextureCache); so even you use ap...

adding particles Effect in pixijs using https://pixijs.io/pixi-particles-editor/

adding particle in pixijs is very easy using the below tool more information can be found below https://github.com/pixijs/pixi-particles https://pixijs.io/pixi-particles-editor/ required packages  /// < reference path = "node_modules/pixi-particles/ambient.d.ts" /> import 'pixi-particles' code of particle delcare a     global variable   private emitter ?: Emitter ; const img = PIXI . Texture . from ( "./assets/images/particle.png" ); this . emitter = new Emitter ( this ,[ img ],{ "alpha" : { "start" : 0.62 , "end" : 0.39 }, "scale" : { "start" : 0.1 , "end" : 0.9 , "minimumScaleMultiplier" : 1.25 }, "color" : { "start" : "#ffff8f" , "end" : ...