Skip to content

Commit

Permalink
merge in #25 and match coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
kgn committed Dec 23, 2013
2 parents d5e3994 + 6eef3b5 commit a3c6591
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
31 changes: 29 additions & 2 deletions ExampleApp/KGAppDelegate.m
Expand Up @@ -28,6 +28,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[showButton addTarget:self action:@selector(showAction:) forControlEvents:UIControlEventTouchUpInside];
[self.window.rootViewController.view addSubview:showButton];

[KGModal sharedInstance].closeButtonType = KGModalCloseButtonTypeRight;

[self.window makeKeyAndVisible];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShow:) name:KGModalWillShowNotification object:nil];
Expand All @@ -39,7 +41,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
}

- (void)showAction:(id)sender{
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 200)];
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 250)];

CGRect welcomeLabelRect = contentView.bounds;
welcomeLabelRect.origin.y = 20;
Expand All @@ -57,7 +59,7 @@ - (void)showAction:(id)sender{

CGRect infoLabelRect = CGRectInset(contentView.bounds, 5, 5);
infoLabelRect.origin.y = CGRectGetMaxY(welcomeLabelRect)+5;
infoLabelRect.size.height -= CGRectGetMinY(infoLabelRect);
infoLabelRect.size.height -= CGRectGetMinY(infoLabelRect) + 50;
UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];
infoLabel.text = @"KGModal is an easy drop in control that allows you to display any view "
"in a modal popup. The modal will automatically scale to fit the content view "
Expand All @@ -69,6 +71,14 @@ - (void)showAction:(id)sender{
infoLabel.shadowColor = [UIColor blackColor];
infoLabel.shadowOffset = CGSizeMake(0, 1);
[contentView addSubview:infoLabel];

CGFloat btnY = CGRectGetMaxY(infoLabelRect)+5;
CGFloat btnH = CGRectGetMaxY(contentView.frame)-5 - btnY;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(infoLabelRect.origin.x, btnY, infoLabelRect.size.width, btnH);
[btn setTitle:@"Close Button Right" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(changeCloseButtonType:) forControlEvents:UIControlEventTouchUpInside];
[contentView addSubview:btn];

// [[KGModal sharedInstance] setCloseButtonLocation:KGModalCloseButtonLocationRight];
[[KGModal sharedInstance] showWithContentView:contentView andAnimated:YES];
Expand All @@ -90,4 +100,21 @@ - (void)didHide:(NSNotification *)notification{
NSLog(@"did hide");
}

- (void)changeCloseButtonType:(id)sender{
UIButton *button = (UIButton *)sender;
KGModal *modal = [KGModal sharedInstance];
KGModalCloseButtonType type = modal.closeButtonType;

if(type == KGModalCloseButtonTypeLeft){
modal.closeButtonType = KGModalCloseButtonTypeRight;
[button setTitle:@"Close Button Right" forState:UIControlStateNormal];
}else if(type == KGModalCloseButtonTypeRight){
modal.closeButtonType = KGModalCloseButtonTypeNone;
[button setTitle:@"Close Button None" forState:UIControlStateNormal];
}else{
modal.closeButtonType = KGModalCloseButtonTypeLeft;
[button setTitle:@"Close Button Left" forState:UIControlStateNormal];
}
}

@end
17 changes: 7 additions & 10 deletions KGModal.h
Expand Up @@ -18,9 +18,10 @@ typedef NS_ENUM(NSUInteger, KGModalBackgroundDisplayStyle){
KGModalBackgroundDisplayStyleSolid
};

typedef NS_ENUM(NSUInteger, KGModalCloseButtonLocation){
KGModalCloseButtonLocationLeft,
KGModalCloseButtonLocationRight
typedef NS_ENUM(NSUInteger, KGModalCloseButtonType){
KGModalCloseButtonTypeNone,
KGModalCloseButtonTypeLeft,
KGModalCloseButtonTypeRight
};

@interface KGModal : NSObject
Expand All @@ -33,13 +34,9 @@ typedef NS_ENUM(NSUInteger, KGModalCloseButtonLocation){
// Defaults to YES
@property (nonatomic) BOOL animateWhenDismissed;

// Determines if the close button is shown
// Defaults to YES
@property (nonatomic) BOOL showCloseButton;

// Determines whether close button will display on the left or right
// Defaults to left
@property (nonatomic) KGModalCloseButtonLocation closeButtonLocation;
// Determins close button type (None/Left/Right)
// Defaults to Left
@property (nonatomic) KGModalCloseButtonType closeButtonType;

// The background color of the modal window
// Defaults black with 0.5 opacity
Expand Down
29 changes: 21 additions & 8 deletions KGModal.m
Expand Up @@ -12,6 +12,8 @@
CGFloat const kFadeInAnimationDuration = 0.3;
CGFloat const kTransformPart1AnimationDuration = 0.2;
CGFloat const kTransformPart2AnimationDuration = 0.1;
CGFloat const kDefaultCloseButtonPadding = 17.0;

NSString *const KGModalGradientViewTapped = @"KGModalGradientViewTapped";

NSString *const KGModalWillShowNotification = @"KGModalWillShowNotification";
Expand Down Expand Up @@ -63,17 +65,26 @@ - (instancetype)init{
self.shouldRotate = YES;
self.tapOutsideToDismiss = YES;
self.animateWhenDismissed = YES;
self.closeButtonLocation = KGModalCloseButtonLocationLeft;
self.showCloseButton = YES;
self.closeButtonType = KGModalCloseButtonTypeLeft;
self.modalBackgroundColor = [UIColor colorWithWhite:0 alpha:0.5];

return self;
}

- (void)setShowCloseButton:(BOOL)showCloseButton{
if(_showCloseButton != showCloseButton){
_showCloseButton = showCloseButton;
[self.closeButton setHidden:!self.showCloseButton];
-(void)setCloseButtonType:(KGModalCloseButtonType)closeButtonType {
_closeButtonType = closeButtonType;
if(closeButtonType == KGModalCloseButtonTypeNone){
[self.closeButton setHidden:YES];
}else{
[self.closeButton setHidden:NO];

CGRect closeFrame = self.closeButton.frame;
if(closeButtonType == KGModalCloseButtonTypeRight){
closeFrame.origin.x = round(CGRectGetWidth(self.containerView.frame)-kDefaultCloseButtonPadding-CGRectGetWidth(closeFrame)/2);
}else{
closeFrame.origin.x = 0;
}
self.closeButton.frame = closeFrame;
}
}

Expand Down Expand Up @@ -116,17 +127,19 @@ - (void)showWithContentView:(UIView *)contentView andAnimated:(BOOL)animated {

KGModalCloseButton *closeButton = [[KGModalCloseButton alloc] init];

if(self.closeButtonLocation == KGModalCloseButtonLocationRight){
if(self.closeButtonType == KGModalCloseButtonTypeRight){
CGRect closeFrame = closeButton.frame;
closeFrame.origin.x = CGRectGetWidth(containerView.bounds)-CGRectGetWidth(closeFrame);
closeButton.frame = closeFrame;
}

[closeButton addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
[closeButton setHidden:!self.showCloseButton];
[containerView addSubview:closeButton];
self.closeButton = closeButton;

// Force adjust visibility and placing
[self setCloseButtonType:self.closeButtonType];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tapCloseAction:)
name:KGModalGradientViewTapped object:nil];

Expand Down
6 changes: 3 additions & 3 deletions readme.md
Expand Up @@ -19,9 +19,9 @@ There are a couple other options but it's purposely designed to be simple and ea
// Defaults to YES
@property (nonatomic) BOOL animateWhenDismissed;
// Determines if the close button is shown
// Defaults to YES
@property (nonatomic) BOOL showCloseButton;
// Determins close button type (None/Left/Right)
// Defaults to Left
@property (nonatomic) KGModalCloseButtonType closeButtonType;
// Determines whether close button will display on the left or right
// Defaults to left
Expand Down

0 comments on commit a3c6591

Please sign in to comment.