Skip to content

Commit

Permalink
- Improved background-tap dismiss action of sheet using TapActionDismiss
Browse files Browse the repository at this point in the history
- Fixed tapDismissAction broken after 2.5.1
  • Loading branch information
Noor ul Ain Ali committed Dec 19, 2020
1 parent e0f58d8 commit a814f2e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ typedef NS_ENUM(NSInteger, ActionType) {
};

typedef NS_ENUM(NSInteger, TapAction) {
TapActionNone,
TapActionDismiss,
TapActionSuccess,
TapActionCancel
};
Expand Down Expand Up @@ -81,7 +81,7 @@ static NSString *const kActionTarget = @"buttonActionTarget";
@property(nonatomic) NSNumber *pickerBlurRadius;
@property(nonatomic, retain) Class popoverBackgroundViewClass; //allow popover customization on iPad
@property(nonatomic) UIInterfaceOrientationMask supportedInterfaceOrientations; // You can set your own supportedInterfaceOrientations value to prevent dismissing picker in some special cases.
@property(nonatomic) TapAction tapDismissAction; // Specify, which action should be fired in case of tapping outside of the picker (on top darkened side). Default is TapActionNone.
@property(nonatomic) TapAction tapDismissAction; // Specify, which action should be fired in case of tapping outside of the picker (on top darkened side). Default is TapActionDismiss.
@property(nonatomic) BOOL popoverDisabled; // Disable popover behavior on iPad


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ - (instancetype)init {
[self setCancelBarButtonItem:sysCancelButton];
[self setDoneBarButtonItem:sysDoneButton];

self.tapDismissAction = TapActionNone;
self.tapDismissAction = TapActionDismiss;
//allows us to use this without needing to store a reference in calling class
self.selfReference = self;

Expand Down Expand Up @@ -310,18 +310,24 @@ - (void)showActionSheetPicker {
#pragma ide diagnostic ignored "UnavailableInDeploymentTarget"
{
switch (self.tapDismissAction) {
case TapActionNone:
case TapActionDismiss: {
// add tap dismiss action
self.actionSheet.window.userInteractionEnabled = YES;
UITapGestureRecognizer *tapAction = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissPicker)];
tapAction.delegate = self;
[self.actionSheet.window addGestureRecognizer:tapAction];
break;
}
case TapActionSuccess: {
// add tap dismiss action
// add tap success action with dismissPicker
self.actionSheet.window.userInteractionEnabled = YES;
UITapGestureRecognizer *tapAction = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionPickerDone:)];
tapAction.delegate = self;
[self.actionSheet.window addGestureRecognizer:tapAction];
break;
}
case TapActionCancel: {
// add tap dismiss action
// add tap cancel action with dismissPicker
self.actionSheet.window.userInteractionEnabled = YES;
UITapGestureRecognizer *tapAction = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionPickerCancel:)];
tapAction.delegate = self;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
static const enum UIViewAnimationOptions options = UIViewAnimationOptionCurveEaseIn;


@interface SWActionSheetVC : UIViewController <UIGestureRecognizerDelegate>
@interface SWActionSheetVC : UIViewController

@property (nonatomic, retain) SWActionSheet *actionSheet;
@property (nonatomic, retain) UITapGestureRecognizer *dismissTap;

@end

Expand Down Expand Up @@ -189,11 +188,6 @@ - (void)showInContainerViewAnimated:(BOOL)animated
self.presented = YES;
}

- (void)dismissActionSheet
{
[self dismissWithClickedButtonIndex:0 animated:YES];
}

@end


Expand Down Expand Up @@ -235,31 +229,14 @@ - (void)presentActionSheetAnimated:(BOOL)animated
_actionSheet.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_actionSheet];
[_actionSheet showInContainerViewAnimated:animated];

// Add Tap Gesture on Background to dismiss ActionSheet
[_actionSheet removeGestureRecognizer:self.dismissTap];
self.dismissTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissActionSheet)];
self.dismissTap.delegate = self;
[_actionSheet addGestureRecognizer:self.dismissTap];
}
}

- (void)dismissActionSheet
{
[_actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

- (BOOL)prefersStatusBarHidden
{
return [UIApplication sharedApplication].statusBarHidden;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
CGPoint location = [touch locationInView:_actionSheet];
return !CGRectContainsPoint(_actionSheet.bgView.frame, location);
}


#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_6_0
// iOS6 support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,27 @@ class SWTableViewController: UITableViewController, UITextFieldDelegate {
origin: sender.superview!.superview)
}


@IBAction func timePickerClicked(_ sender: UIButton) {
// example of picker initialized with target/action parameters
let datePicker = ActionSheetDatePicker(title: "Time - (Automatic):",
datePickerMode: UIDatePicker.Mode.time,
selectedDate: Date(),
target: self,
action: #selector(datePicked(_:)),
origin: sender.superview!.superview)
let datePicker = ActionSheetDatePicker(
title: "Time - (Automatic):",
datePickerMode: .time,
selectedDate: Date(),
doneBlock: { (datePicker, selectedDate, origin) in
print("DONE: Date picked \(selectedDate ?? "NO DATE")")
},
cancel: { (datePicker) in
print("CANCELLED")
},
origin: sender.superview!.superview)
datePicker?.minuteInterval = 20
if #available(iOS 13.4, *) {
datePicker?.datePickerStyle = .automatic
}

datePicker?.tapDismissAction = .cancel
datePicker?.show()
}

@objc func datePicked(_ date: Date) {
print("Date picked \(date)")
}


@IBAction func datePickerClicked(_ sender: UIButton) {
// example of date picker with min and max values set (as a week in past and week in future from today)
let datePicker = ActionSheetDatePicker(title: "Date within 2 weeks - (Inline):",
Expand Down

0 comments on commit a814f2e

Please sign in to comment.