From a914c2394a8988fc9f18b991546501a93f5d7fc5 Mon Sep 17 00:00:00 2001 From: behlul Date: Thu, 17 Jan 2013 13:17:33 -0800 Subject: [PATCH] - Using NSArray instead of variable list - Adding a method which will allow us to show in a rectangle but still block the whole view - adjusting internal implementation to fit the behaviour in item 2 --- RDActionSheet/RDActionSheet.h | 8 ++++--- RDActionSheet/RDActionSheet.m | 39 +++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/RDActionSheet/RDActionSheet.h b/RDActionSheet/RDActionSheet.h index 27b65d4..5b429b2 100644 --- a/RDActionSheet/RDActionSheet.h +++ b/RDActionSheet/RDActionSheet.h @@ -24,14 +24,16 @@ typedef void(^RDCallbackBlock)(RDActionSheetResult result, NSInteger buttonIndex @property (nonatomic, unsafe_unretained) NSObject *delegate; @property (nonatomic, copy) RDCallbackBlock callbackBlock; -- (id)initWithDelegate:(NSObject *)aDelegate cancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... __attribute__ ((deprecated)); +- (id)initWithDelegate:(NSObject *)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 diff --git a/RDActionSheet/RDActionSheet.m b/RDActionSheet/RDActionSheet.m index a66fd2a..90c19ea 100644 --- a/RDActionSheet/RDActionSheet.m +++ b/RDActionSheet/RDActionSheet.m @@ -63,9 +63,9 @@ - (id)init { return self; } -- (id)initWithDelegate:(NSObject *)aDelegate cancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... { +- (id)initWithDelegate:(NSObject *)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; @@ -74,26 +74,18 @@ - (id)initWithDelegate:(NSObject *)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]; @@ -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]; @@ -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; @@ -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 {