iOS CustomDialog
This is the first step of custom dialog.
No UI in there.
We can customize to add UI in UIView.
This code is inspired from wimagguc / ios-custom-alertview. This is nice codes for it.
I simplified and clean and remove all UI inside of Dialog. That is.
Sample
CustomDialogView
#import <UIKit/UIKit.h> @interface CustomDialogView : UIView @property(nonatomic, weak)UIView *parentView; @property(nonatomic) UIView *dialogView; @property(nonatomic) UIView *containerView; @property(nonatomic) double dialog_width; @property(nonatomic) double dialog_height; - (id)init; - (id)initWithParentView: (UIView *)parent; - (void)show; - (void)close; @end
CustomDialogView.m
#import "CustomDialogView.h"
#define kCustomCornerRadius 7
@implementation CustomDialogView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithParentView: (UIView *)parent
{
self = [self init];
if (parent != nil) {
self.frame = parent.frame;
self.parentView = parent;
}
return self;
}
- (id)init
{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}
return self;
}
#pragma mark - Dialog Events
- (void)show
{
self.dialogView = [self createContainerView];
self.dialogView.layer.shouldRasterize = YES;
self.dialogView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = [[UIScreen mainScreen] scale];
self.dialogView.layer.opacity = 0.5f;
self.dialogView.layer.transform = CATransform3DMakeScale(1.3f, 1.3f, 1.0);
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
UITapGestureRecognizer *touchGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapped:)];
[self addGestureRecognizer:touchGesture];
[self addSubview:self.dialogView];
if (self.parentView != nil) {
[self.parentView addSubview:self];
}
[UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4f];
self.dialogView.layer.opacity = 1.0f;
self.dialogView.layer.transform = CATransform3DMakeScale(1, 1, 1);
}
completion:NULL
];
}
- (void)close
{
CATransform3D currentTransform = self.dialogView.layer.transform;
CGFloat startRotation = [[self.dialogView valueForKeyPath:@"layer.transform.rotation.z"] floatValue];
CATransform3D rotation = CATransform3DMakeRotation(-startRotation + M_PI * 270.0 / 180.0, 0.0f, 0.0f, 0.0f);
self.dialogView.layer.transform = CATransform3DConcat(rotation, CATransform3DMakeScale(1, 1, 1));
self.dialogView.layer.opacity = 1.0f;
[UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
animations:^{
self.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f];
self.dialogView.layer.transform = CATransform3DConcat(currentTransform, CATransform3DMakeScale(0.6f, 0.6f, 1.0));
self.dialogView.layer.opacity = 0.0f;
}
completion:^(BOOL finished) {
for (UIView *v in [self subviews]) {
[v removeFromSuperview];
}
[self removeFromSuperview];
}
];
}
-(void)backgroundTapped:(id)sender {
[self close];
}
-(void)dialogTapped:(id)sender {
}
#pragma mark - UIView
- (UIView *)createContainerView {
UIView *dialogContainer = [[UIView alloc] initWithFrame:CGRectMake((self.frame.size.width - self.dialog_width) / 2, ( self.frame.size.height - self.dialog_height) / 2, self.dialog_width, self.dialog_height)];
// First, we style the dialog to match the iOS7 UIAlertView >>>
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = dialogContainer.bounds;
gradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:218.0/255.0 green:218.0/255.0 blue:218.0/255.0 alpha:1.0f] CGColor],
(id)[[UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1.0f] CGColor],
(id)[[UIColor colorWithRed:218.0/255.0 green:218.0/255.0 blue:218.0/255.0 alpha:1.0f] CGColor],
nil];
CGFloat cornerRadius = kCustomCornerRadius;
gradient.cornerRadius = cornerRadius;
[dialogContainer.layer insertSublayer:gradient atIndex:0];
dialogContainer.layer.cornerRadius = cornerRadius;
dialogContainer.layer.borderColor = [[UIColor colorWithRed:198.0/255.0 green:198.0/255.0 blue:198.0/255.0 alpha:1.0f] CGColor];
dialogContainer.layer.borderWidth = 1;
dialogContainer.layer.shadowRadius = cornerRadius + 5;
dialogContainer.layer.shadowOpacity = 0.1f;
dialogContainer.layer.shadowOffset = CGSizeMake(0 - (cornerRadius+5)/2, 0 - (cornerRadius+5)/2);
dialogContainer.layer.shadowColor = [UIColor blackColor].CGColor;
dialogContainer.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:dialogContainer.bounds cornerRadius:dialogContainer.layer.cornerRadius].CGPath;
UITapGestureRecognizer *touchGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dialogTapped:)];
[dialogContainer addGestureRecognizer:touchGesture];
// This is custom part
return dialogContainer;
}

