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

showing sheet in a specific rectangle, while blocking the whole view #10

Open
wants to merge 1 commit 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
8 changes: 5 additions & 3 deletions RDActionSheet/RDActionSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ typedef void(^RDCallbackBlock)(RDActionSheetResult result, NSInteger buttonIndex
@property (nonatomic, unsafe_unretained) NSObject <RDActionSheetDelegate> *delegate;
@property (nonatomic, copy) RDCallbackBlock callbackBlock;

- (id)initWithDelegate:(NSObject <RDActionSheetDelegate> *)aDelegate cancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... __attribute__ ((deprecated));
- (id)initWithDelegate:(NSObject <RDActionSheetDelegate> *)aDelegate cancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles __attribute__ ((deprecated));

- (id)initWithCancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;
- (id)initWithCancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles;

- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;
- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray*)otherButtonTitles;

- (void)showFrom:(UIView *)view;

-(void)showFrom:(UIView*)view inRect:(CGRect)rect;

- (void)cancelActionSheet;

@end
Expand Down
39 changes: 23 additions & 16 deletions RDActionSheet/RDActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ - (id)init {
return self;
}

- (id)initWithDelegate:(NSObject<RDActionSheetDelegate> *)aDelegate cancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
- (id)initWithDelegate:(NSObject<RDActionSheetDelegate> *)aDelegate cancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray*)otherButtonTitles {

self = [self initWithCancelButtonTitle:cancelButtonTitle primaryButtonTitle:primaryButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:otherButtonTitles, nil];
self = [self initWithCancelButtonTitle:cancelButtonTitle primaryButtonTitle:primaryButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:otherButtonTitles];

if (self) {
self.delegate = aDelegate;
Expand All @@ -74,26 +74,18 @@ - (id)initWithDelegate:(NSObject<RDActionSheetDelegate> *)aDelegate cancelButton
return self;
}

- (id)initWithCancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
- (id)initWithCancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray*)otherButtonTitles {

self = [self init];
if (self) {

// Build normal buttons
va_list argumentList;
va_start(argumentList, otherButtonTitles);

NSString *argString = otherButtonTitles;
while (argString != nil) {
for (NSString* title in otherButtonTitles) {

UIButton *button = [self buildButtonWithTitle:argString];
UIButton *button = [self buildButtonWithTitle:title];
[self.buttons addObject:button];

argString = va_arg(argumentList, NSString *);
}

va_end(argumentList);

// Build cancel button
UIButton *cancelButton = [self buildCancelButtonWithTitle:cancelButtonTitle];
[self.buttons insertObject:cancelButton atIndex:0];
Expand All @@ -114,9 +106,9 @@ - (id)initWithCancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle
return self;
}

- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray*)otherButtonTitles {

self = [self initWithCancelButtonTitle:cancelButtonTitle primaryButtonTitle:primaryButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:otherButtonTitles, nil];
self = [self initWithCancelButtonTitle:cancelButtonTitle primaryButtonTitle:primaryButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:otherButtonTitles];

if (self) {
_titleLabel = [self buildTitleLabelWithTitle:title];
Expand Down Expand Up @@ -192,7 +184,7 @@ - (void)setupButtons {
}

button.frame = CGRectMake(0, 0, buttonWidth, kButtonHeight);
button.center = CGPointMake(self.center.x, yOffset);
button.center = CGPointMake(self.frame.size.width/2, yOffset);
[self addSubview:button];

yOffset -= button.frame.size.height + kButtonPadding;
Expand Down Expand Up @@ -390,6 +382,21 @@ - (void)showFrom:(UIView *)view {
}];
}

-(void)showFrom:(UIView*)view inRect:(CGRect)rect {
CGFloat startPosition = rect.origin.y + rect.size.height;
self.frame = CGRectMake(rect.origin.x, startPosition, rect.size.width, [self calculateSheetHeight]);
[view addSubview:self];

self.blackOutView = [self buildBlackOutViewWithFrame:view.bounds];
[view insertSubview:self.blackOutView belowSubview:self];

[UIView animateWithDuration:kActionSheetAnimationTime animations:^{
CGFloat endPosition = startPosition - self.frame.size.height;
self.frame = CGRectMake(self.frame.origin.x, endPosition, self.frame.size.width, self.frame.size.height);
self.blackOutView.alpha = kBlackoutViewFadeInOpacity;
}];
}

#pragma mark - Helpers

- (CGFloat)calculateSheetHeight {
Expand Down