Skip to content
Open
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
133 changes: 130 additions & 3 deletions RDActionSheet/RDActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#import <QuartzCore/QuartzCore.h>


@interface RDActionSheet ()
@interface RDActionSheet () <UIActionSheetDelegate>

@property (nonatomic, weak) UIActionSheet *ios7actionSheet;
@property (nonatomic, strong) UIView *blackOutView;
@property (nonatomic, strong) UILabel *titleLabel;

Expand Down Expand Up @@ -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

Expand All @@ -50,6 +54,7 @@ @implementation RDActionSheet

@synthesize buttons;
@synthesize blackOutView;
@synthesize ios7actionSheet;

#pragma mark - Initialization

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -289,6 +299,7 @@ - (UIButton *)buildCancelButtonWithTitle:(NSString *)title {
[button setBackgroundImage:touchBackgroundImage forState:UIControlStateHighlighted];

button.titleLabel.layer.shadowOpacity = 0.3;
button.tag = kCancelButtonTag;

return button;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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