I am using SWRevealViewController
in order to implement a side nav menu in my app. I would like to make it so that the front view cannot be interacted with when the rear view is opened, except that when the user taps the front view, the rear view will close and the front view can be interacted with again. I have these two SWRevealViewController
delegate methods that currently remove interaction from the front view.
- (void)revealController:(SWRevealViewController *)revealController willMoveToPosition: (FrontViewPosition)position {
if(position == FrontViewPositionLeft) {
self.view.userInteractionEnabled = YES;
} else {
self.view.userInteractionEnabled = NO;
}
}
- (void)revealController:(SWRevealViewController *)revealController didMoveToPosition: (FrontViewPosition)position {
if(position == FrontViewPositionLeft) {
self.view.userInteractionEnabled = YES;
} else {
self.view.userInteractionEnabled = NO;
}
}
However this doesn't cause the rear view to close when the front view is tapped. Any help would be greatly appreciated, thanks!
If you are using SWIFT, you can do something like this, in your frontViewController:
override func viewDidLoad() {
super.viewDidLoad()
if self.revealViewController() != nil {
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
}
}
Code works for TAP and PAN gesture.
In ViewDidLoad of your frontViewController you need to add a UITapGestureRecognizer
SWRevealViewController *revealController = [self revealViewController];
UITapGestureRecognizer *tap = [revealController tapGestureRecognizer];
tap.delegate = self;
[myView addGestureRecognizer:tap];
This should cause the rear view to close when the front view is tapped which is SWRevealViewController
's default behaviour.
EDIT: Change UIView's autoresizingMask to suit Swift 2, thanks to Marroc's comment
This is Swift-SWRevealViewController 2.x version of @avdyushin's answer:
func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
let tagId = 4207868622
if revealController.frontViewPosition == FrontViewPosition.Right {
let lock = self.view.viewWithTag(tagId)
UIView.animateWithDuration(0.25, animations: {
lock?.alpha = 0
}, completion: {(finished: Bool) in
lock?.removeFromSuperview()
}
)
lock?.removeFromSuperview()
} else if revealController.frontViewPosition == FrontViewPosition.Left {
let lock = UIView(frame: self.view.bounds)
lock.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
lock.tag = tagId
lock.alpha = 0
lock.backgroundColor = UIColor.blackColor()
lock.addGestureRecognizer(UITapGestureRecognizer(target: self.revealViewController(), action: "revealToggle:"))
self.view.addSubview(lock)
UIView.animateWithDuration(0.75, animations: {
lock.alpha = 0.333
}
)
}
}
SWRevealViewController
.revealController:willMoveToPosition:
of SWRevealViewControllerDelegate
.Sample with nice animation:
- (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position;
{
static NSInteger tagLockView = 4207868622;
if (revealController.frontViewPosition == FrontViewPositionRight) {
UIView *lock = [self.frontViewController.view viewWithTag:tagLockView];
[UIView animateWithDuration:0.25 animations:^{
lock.alpha = 0;
} completion:^(BOOL finished) {
[lock removeFromSuperview];
}];
} else if (revealController.frontViewPosition == FrontViewPositionLeft) {
UIView *lock = [[UIView alloc] initWithFrame:self.frontViewController.view.bounds];
lock.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
lock.tag = tagLockView;
lock.backgroundColor = [UIColor blackColor];
lock.alpha = 0;
[lock addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(revealToggle:)]];
[self.frontViewController.view addSubview:lock];
[UIView animateWithDuration:0.75 animations:^{
lock.alpha = 0.333;
}];
}
}
This solution for old version 1.x SWRevealViewController.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With