Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to dismiss when touching outside. #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion RNBlurModalView.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,30 @@
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

extern CGFloat kRNBlurDefaultDelay;
extern CGFloat kRNDefaultBlurScale;
extern CGFloat kRNBlurDefaultDuration;
extern CGFloat kRNBlurViewMaxAlpha;
extern CGFloat kRNBlurBounceOutDurationScale;

extern NSString * const kRNBlurDidShowNotification;
extern NSString * const kRNBlurDidHidewNotification;

@interface RNBlurModalView : UIView
@interface RNBlurModalView : UIView <UIGestureRecognizerDelegate>

@property (assign, readonly) BOOL isVisible;

@property (assign) CGFloat animationDuration;
@property (assign) CGFloat animationDelay;
@property (assign) UIViewAnimationOptions animationOptions;
@property (assign) BOOL dismissButtonRight;
@property (nonatomic) BOOL tapOutsideToDismiss;
@property (nonatomic, copy) void (^defaultHideBlock)(void);
@property (assign) CGFloat offsetX;
@property (assign) CGFloat offsetY;
@property (nonatomic, assign) CGAffineTransform startTransform;
@property (nonatomic, assign) CGAffineTransform endTransform;


- (id)initWithViewController:(UIViewController*)viewController view:(UIView*)view;
- (id)initWithViewController:(UIViewController*)viewController title:(NSString*)title message:(NSString*)message;
Expand Down
46 changes: 35 additions & 11 deletions RNBlurModalView.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@
This bit is important! In order to prevent capturing selected states of UIResponders I've implemented a delay. Please feel free to set this delay to *whatever* you deem apprpriate.
I've defaulted it to 0.125 seconds. You can do shorter/longer as you see fit.
*/
CGFloat const kRNBlurDefaultDelay = 0.125f;
CGFloat kRNBlurDefaultDelay = 0.125f;

/*
You can also change this constant to make the blur more "blurry". I recommend the tasteful level of 0.2 and no higher. However, you are free to change this from 0.0 to 1.0.
*/
CGFloat const kRNDefaultBlurScale = 0.2f;
CGFloat kRNDefaultBlurScale = 0.2f;

CGFloat const kRNBlurDefaultDuration = 0.2f;
CGFloat const kRNBlurViewMaxAlpha = 1.f;
CGFloat kRNBlurDefaultDuration = 0.2f;
CGFloat kRNBlurViewMaxAlpha = 1.f;

CGFloat const kRNBlurBounceOutDurationScale = 0.8f;
CGFloat kRNBlurBounceOutDurationScale = 0.8f;

NSString * const kRNBlurDidShowNotification = @"com.whoisryannystrom.RNBlurModalView.show";
NSString * const kRNBlurDidHidewNotification = @"com.whoisryannystrom.RNBlurModalView.hide";
Expand Down Expand Up @@ -139,13 +139,14 @@ + (UIView*)generateModalViewWithTitle:(NSString*)title message:(NSString*)messag
return view;
}


- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_dismissButton = [[RNCloseButton alloc] init];
_dismissButton.center = CGPointZero;
[_dismissButton addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];

self.tapOutsideToDismiss = YES;

self.alpha = 0.f;
self.backgroundColor = [UIColor clearColor];
// self.backgroundColor = [UIColor redColor];
Expand All @@ -158,6 +159,14 @@ - (id)initWithFrame:(CGRect)frame {
UIViewAutoresizingFlexibleTopMargin);

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChangeNotification:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[singleTap setDelegate:self];
[singleTap setNumberOfTapsRequired:1];
[self addGestureRecognizer:singleTap];

self.startTransform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4);
self.endTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, 1.f);
}
return self;
}
Expand All @@ -168,7 +177,7 @@ - (id)initWithViewController:(UIViewController*)viewController view:(UIView*)vie
if (self = [self initWithFrame:CGRectMake(0, 0, viewController.view.width, viewController.view.height)]) {
[self addSubview:view];
_contentView = view;
_contentView.center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
_contentView.center = CGPointMake(CGRectGetMidX(self.frame) - self.offsetX, CGRectGetMidY(self.frame) - self.offsetY);
_controller = viewController;
_parentView = nil;
_contentView.clipsToBounds = YES;
Expand All @@ -193,7 +202,7 @@ - (id)initWithParentView:(UIView*)parentView view:(UIView*)view {
if (self = [self initWithFrame:CGRectMake(0, 0, parentView.width, parentView.height)]) {
[self addSubview:view];
_contentView = view;
_contentView.center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
_contentView.center = CGPointMake(CGRectGetMidX(self.frame) - self.offsetX, CGRectGetMidY(self.frame) - self.offsetY);
_controller = nil;
_parentView = parentView;
_contentView.clipsToBounds = YES;
Expand Down Expand Up @@ -272,8 +281,8 @@ - (void)updateSubviews {


self.hidden = NO;

_contentView.center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
_contentView.center = CGPointMake(CGRectGetMidX(self.frame) - self.offsetX, CGRectGetMidY(self.frame) - self.offsetY);
_dismissButton.center = _contentView.origin;
}

Expand Down Expand Up @@ -328,8 +337,12 @@ - (void)delayedShow {

[_parentView insertSubview:_blurView belowSubview:self];
}

_contentView.center = CGPointMake(CGRectGetMidX(self.frame) - self.offsetX, CGRectGetMidY(self.frame) - self.offsetY);
_dismissButton.center = _contentView.origin;

self.transform = self.startTransform;

self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4);
[UIView animateWithDuration:self.animationDuration animations:^{
_blurView.alpha = 1.f;
self.alpha = 1.f;
Expand Down Expand Up @@ -362,6 +375,7 @@ - (void)hideWithDuration:(CGFloat)duration delay:(NSTimeInterval)delay options:(
animations:^{
self.alpha = 0.f;
_blurView.alpha = 0.f;
self.transform = self.endTransform;
}
completion:^(BOOL finished){
if (finished) {
Expand All @@ -383,6 +397,16 @@ -(void)hideCloseButton:(BOOL)hide {
[_dismissButton setHidden:hide];
}

-(void) handleTap:(UITapGestureRecognizer *) recognizer {
if(self.tapOutsideToDismiss){
[self hide];
}
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return !(touch.view == _contentView || touch.view == _dismissButton);
}

@end

#pragma mark - RNBlurView
Expand Down