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

Method for presenting overlapping views. #32

Merged
merged 2 commits into from
Sep 4, 2014
Merged
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
2 changes: 1 addition & 1 deletion Pickers/AbstractActionSheetPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#import <objc/message.h>
#import <sys/utsname.h>

BOOL isIPhone4()
CG_INLINE BOOL isIPhone4()
{
struct utsname systemInfo;
uname(&systemInfo);
Expand Down
2 changes: 0 additions & 2 deletions Pickers/SWActionSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@


@interface SWActionSheet : UIView
@property(nonatomic, strong) UIView *origin;

@property(nonatomic, strong) UIView *bgView;

- (void)dismissWithClickedButtonIndex:(int)i animated:(BOOL)animated;
Expand Down
169 changes: 133 additions & 36 deletions Pickers/SWActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,114 @@
#import "SWActionSheet.h"


static UIWindow *SWActionSheetWindow = nil;

static const float delay = 0.f;

static const float duration = .3f;

static const enum UIViewAnimationOptions options = UIViewAnimationOptionCurveEaseIn;


@interface SWActionSheetVC : UIViewController

@property (nonatomic, retain) SWActionSheet *actionSheet;

@end


@interface SWActionSheet ()

@property (nonatomic, assign) BOOL presented;

- (void)configureFrameForBounds:(CGRect)bounds;
- (void)showInContainerViewAnimated:(BOOL)animated;

@end


@implementation SWActionSheet
{
UIView *view;

UIView *_origin;
UIView *_bgView;
}

- (void)dismissWithClickedButtonIndex:(int)i animated:(BOOL)animated
{
CGPoint fadeOutToPoint = CGPointMake(view.center.x,
self.center.y + CGRectGetHeight(view.frame));
// Window of app
UIWindow *appWindow = [UIApplication sharedApplication].windows.firstObject;
// Actions
void (^actions)() = ^{
self.center = fadeOutToPoint;
self.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.0f];
};
void (^completion)(BOOL) = ^(BOOL finished) {
if (![appWindow isKeyWindow])
[appWindow makeKeyAndVisible];
[SWActionSheet destroyWindow];
[self removeFromSuperview];
};
// Do actions animated or not
if (animated) {
[UIView animateWithDuration:duration delay:delay options:options animations:actions completion:completion];
} else {
actions();
completion(YES);
}
self.presented = NO;
}

[UIView animateWithDuration:duration delay:delay
options:options animations:^{
self.center = fadeOutToPoint;
self.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.0f];
}
completion:^(BOOL finished) {
[self removeFromSuperview];
}];
+ (void)destroyWindow
{
if (SWActionSheetWindow)
{
[SWActionSheet actionSheetContainer].actionSheet = nil;
SWActionSheetWindow.hidden = YES;
if ([SWActionSheetWindow isKeyWindow])
[SWActionSheetWindow resignFirstResponder];
SWActionSheetWindow = nil;
}
}

- (instancetype)initWithView:(UIView *)aView
+ (UIWindow *)window
{
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
_origin = (keyWindow.rootViewController ? keyWindow.rootViewController.view : keyWindow);
return (SWActionSheetWindow ?: (SWActionSheetWindow = ({
UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.windowLevel = UIWindowLevelAlert;
window.backgroundColor = [UIColor clearColor];
window.rootViewController = [SWActionSheetVC new];
window;
})));
}

+ (SWActionSheetVC *)actionSheetContainer
{
return (SWActionSheetVC *) [SWActionSheet window].rootViewController;
}

CGRect rect = [self getRectForPicker:aView];
self = [super initWithFrame:rect];
if ( self )
- (instancetype)initWithView:(UIView *)aView
{
if ((self = [super init]))
{
view = aView;
CGRect cgRect = CGRectMake(view.bounds.origin.x, _origin.bounds.size.height, view.bounds.size.width, view.bounds.size.height);
view.frame = cgRect;
self.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.0f];
_bgView = [[UIView alloc] initWithFrame:view.frame];
_bgView = [UIView new];
_bgView.backgroundColor = [UIColor whiteColor];
[self addSubview:_bgView];
[self addSubview:aView];

[self addSubview:view];
}

return self;
}

- (CGRect)getRectForPicker:(UIView *)aView
- (void)configureFrameForBounds:(CGRect)bounds
{
CGRect rect;
rect = CGRectMake(_origin.bounds.origin.x, _origin.bounds.origin.y, _origin.bounds.size.width, _origin.bounds.size.height + aView.bounds.size.height);
return rect;
self.frame = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height + view.bounds.size.height);
view.frame = CGRectMake(view.bounds.origin.x, bounds.size.height, view.bounds.size.width, view.bounds.size.height);
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
_bgView.frame = view.frame;
_bgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated
Expand All @@ -70,25 +122,70 @@ - (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated

- (void)showInContainerView
{
[_origin addSubview:self];
// Make sheet window visible and active
UIWindow *sheetWindow = [SWActionSheet window];
if (![sheetWindow isKeyWindow])
[sheetWindow makeKeyAndVisible];
sheetWindow.hidden = NO;
// Put our ActionSheet in Container (it will be presented as soon as possible)
[SWActionSheet actionSheetContainer].actionSheet = self;
}

- (void)showInContainerViewAnimated:(BOOL)animated
{
CGPoint toPoint;
CGFloat y = self.center.y - CGRectGetHeight(view.frame);
toPoint = CGPointMake(self.center.x, y);
// Present actions
void (^animations)() = ^{
self.center = toPoint;
self.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.5f];
};
// Present sheet
if (animated)
[UIView animateWithDuration:duration delay:delay options:options animations:animations completion:nil];
else
animations();
self.presented = YES;
}

@end


#pragma mark - SWActionSheet Container

[UIView animateWithDuration:duration delay:delay
options:options animations:^{
self.center = toPoint;
self.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.5f];
}
completion:^(BOOL finished) {
@implementation SWActionSheetVC

}];
- (void)setActionSheet:(SWActionSheet *)actionSheet
{
// Prevent processing one action sheet twice
if (_actionSheet == actionSheet)
return;
// Dissmiss previous action sheet if it presented
if (_actionSheet.presented)
[_actionSheet dismissWithClickedButtonIndex:0 animated:YES];
// Remember new action sheet
_actionSheet = actionSheet;
// Present new action sheet
[self presentActionSheetAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self presentActionSheetAnimated:YES];
}

- (BOOL)isViewPortrait
- (void)presentActionSheetAnimated:(BOOL)animated
{
return UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
// New action sheet will be presented only when view controller will be loaded
if (_actionSheet && [self isViewLoaded] && !_actionSheet.presented)
{
[_actionSheet configureFrameForBounds:self.view.bounds];
_actionSheet.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_actionSheet];
[_actionSheet showInContainerViewAnimated:animated];
}
}

@end