diff --git a/RDActionSheet/RDActionSheet.m b/RDActionSheet/RDActionSheet.m index 4da361a..798041f 100644 --- a/RDActionSheet/RDActionSheet.m +++ b/RDActionSheet/RDActionSheet.m @@ -10,8 +10,9 @@ #import -@interface RDActionSheet () +@interface RDActionSheet () +@property (nonatomic, weak) UIActionSheet *ios7actionSheet; @property (nonatomic, strong) UIView *blackOutView; @property (nonatomic, strong) UILabel *titleLabel; @@ -42,6 +43,9 @@ - (void)buttonWasPressed:(id)button; const CGFloat kActionSheetAnimationTime = 0.2; const CGFloat kBlackoutViewFadeInOpacity = 0.6; +const NSInteger kCancelButtonTag = 23; +const NSInteger kDestructiveButtonTag = 25; +const NSInteger kNormalButtonTag = 12; @implementation RDActionSheet @@ -50,6 +54,7 @@ @implementation RDActionSheet @synthesize buttons; @synthesize blackOutView; +@synthesize ios7actionSheet; #pragma mark - Initialization @@ -126,6 +131,10 @@ - (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButton - (void)layoutSubviews { + if(YES || floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { + return; + } + [self setupBackground]; [self setupTitle]; [self setupButtons]; @@ -180,7 +189,7 @@ - (void)setupButtons { for (UIButton *button in self.buttons) { CGFloat buttonWidth; - UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; + UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) { buttonWidth = kLandscapeButtonWidth; } @@ -205,7 +214,7 @@ - (void)setupButtons { - (void)setupTitle { CGFloat labelWidth; - UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; + UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) { labelWidth = kLandscapeButtonWidth; } @@ -274,6 +283,7 @@ - (UIButton *)buildButtonWithTitle:(NSString *)title { button.titleLabel.layer.shadowRadius = 0.0; button.titleLabel.layer.shadowOffset = CGSizeMake(0.0, 1.0); button.titleLabel.layer.shadowOpacity = 0.5; + button.tag = kNormalButtonTag; return button; } @@ -289,6 +299,7 @@ - (UIButton *)buildCancelButtonWithTitle:(NSString *)title { [button setBackgroundImage:touchBackgroundImage forState:UIControlStateHighlighted]; button.titleLabel.layer.shadowOpacity = 0.3; + button.tag = kCancelButtonTag; return button; } @@ -306,6 +317,7 @@ - (UIButton *)buildPrimaryButtonWithTitle:(NSString *)title { button.titleLabel.layer.shadowColor = [UIColor blackColor].CGColor; button.titleLabel.layer.shadowOffset = CGSizeMake(0.0, -1.0); + button.tag = kNormalButtonTag; return button; } @@ -323,6 +335,7 @@ - (UIButton *)buildDestroyButtonWithTitle:(NSString *)title { button.titleLabel.layer.shadowColor = [UIColor blackColor].CGColor; button.titleLabel.layer.shadowOffset = CGSizeMake(0.0, -1.0); + button.tag = kDestructiveButtonTag; return button; } @@ -379,13 +392,56 @@ - (void)hideActionSheetWithButtonIndex:(NSInteger)buttonIndex { } -(void)cancelActionSheet { + if(YES || floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { + [self.ios7actionSheet dismissWithClickedButtonIndex:-1 animated:YES]; + return; + } [self hideActionSheetWithButtonIndex:-1]; } #pragma mark - Present action sheet - (void)showFrom:(UIView *)view { + + if(YES || floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { + NSInteger destructiveIndex = NSNotFound; + NSInteger cancelIndex = NSNotFound; + + UIActionSheet *actionSheet = [[UIActionSheet alloc] init]; + actionSheet.title = self.titleLabel.text; + + NSInteger index = 0; + for(UIButton *button in [self.buttons reverseObjectEnumerator]) { + switch (button.tag) { + case kDestructiveButtonTag: + destructiveIndex = index; + break; + case kCancelButtonTag: + cancelIndex = index; + break; + default: + break; + } + [actionSheet addButtonWithTitle: [button titleForState:UIControlStateNormal]]; + index++; + } + + if(destructiveIndex != NSNotFound) + actionSheet.destructiveButtonIndex = destructiveIndex; + + if(cancelIndex != NSNotFound) + actionSheet.cancelButtonIndex = cancelIndex; + + actionSheet.delegate = self; + [actionSheet showInView: view]; + + [view addSubview: self]; + + self.ios7actionSheet = actionSheet; + return; + } + CGFloat startPosition = view.bounds.origin.y + view.bounds.size.height; self.frame = CGRectMake(0, startPosition, view.bounds.size.width, [self calculateSheetHeight]); [view addSubview:self]; @@ -426,4 +482,75 @@ - (CGFloat)calculateSheetHeight { return floorf((kButtonHeight * self.buttons.count) + (self.buttons.count * kButtonPadding) + kButtonHeight/2) + self.titleLabel.bounds.size.height + 4; } +#pragma mark - UIActionSheetDelegate methods for iOS7 + +// Called when a button is clicked. The view will be automatically dismissed after this call returns +- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { + + NSInteger rdActionSheetIndex = [self.buttons count]-1-buttonIndex; + + if (self.callbackBlock) { + self.callbackBlock(RDActionSheetCallbackTypeClickedButtonAtIndex, rdActionSheetIndex, [[[self.buttons objectAtIndex:rdActionSheetIndex] titleLabel] text]); + } + else { + if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) { + [self.delegate actionSheet:self clickedButtonAtIndex:rdActionSheetIndex]; + } + } +} + +- (void)willPresentActionSheet:(UIActionSheet *)actionSheet { + if (self.callbackBlock) { + self.callbackBlock(RDActionSheetCallbackTypeWillPresentActionSheet, -1, nil); + } + else { + if (self.delegate && [self.delegate respondsToSelector:@selector(willPresentActionSheet:)]) { + [self.delegate willPresentActionSheet:self]; + } + } +} + +- (void)didPresentActionSheet:(UIActionSheet *)actionSheet{ + if (self.callbackBlock) { + self.callbackBlock(RDActionSheetCallbackTypeDidPresentActionSheet, -1, nil); + } + else { + if (self.delegate && [self.delegate respondsToSelector:@selector(didPresentActionSheet:)]) { + [self.delegate didPresentActionSheet:self]; + } + } +} + +- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex { + if(buttonIndex < 0) + return; + NSInteger rdActionSheetIndex = [self.buttons count]-1-buttonIndex; + if (self.callbackBlock) { + self.callbackBlock(RDActionSheetCallbackTypeWillDismissWithButtonIndex, rdActionSheetIndex, [[[self.buttons objectAtIndex:rdActionSheetIndex] titleLabel] text]); + } + else { + if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:willDismissWithButtonIndex:)]) { + [self.delegate actionSheet:self willDismissWithButtonIndex:rdActionSheetIndex]; + } + } +} + +- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ + if(buttonIndex < 0) + return; + NSInteger rdActionSheetIndex = [self.buttons count]-1-buttonIndex; + if (self.callbackBlock) { + self.callbackBlock(RDActionSheetCallbackTypeDidDismissWithButtonIndex, rdActionSheetIndex, [[[self.buttons objectAtIndex:rdActionSheetIndex] titleLabel] text]); + } + else { + if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:didDismissWithButtonIndex:)]) { + [self.delegate actionSheet:self didDismissWithButtonIndex:rdActionSheetIndex]; + } + } + + if(self.superview != nil) { + [self removeFromSuperview]; + } +} + @end