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
97 changes: 94 additions & 3 deletions Source/SlideMenuController.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ -(instancetype)init {
static BOOL RPSWasOpenAtStartOfPan = NO;
static BOOL RPSWasHiddenAtStartOfPan = NO;
static UIGestureRecognizerState RPSLastState = UIGestureRecognizerStateEnded;
static void *SlideViewObserverContext = @"SlideMenuOptionsSlideViewContext";

@interface SlideMenuController () {

Expand All @@ -79,6 +80,7 @@ -(instancetype)init {
_leftContainerView = [UIView new];
_rightContainerView = [UIView new];
options = [[SlideMenuOption alloc] init];
[self addSlideViewWidthObserver];
}
return self;
}
Expand Down Expand Up @@ -122,6 +124,10 @@ -(instancetype)initWithMainViewController:(UIViewController *)tMainController le
return self;
}

- (void)dealloc {
[self removeSlideViewWidthObserver];
}

-(void)awakeFromNib {
[self initView];
}
Expand Down Expand Up @@ -173,9 +179,87 @@ -(SlideMenuOption *)option {
}

-(void)setOption:(SlideMenuOption *)option {
[self removeSlideViewWidthObserver];
options = option;
[self _changeLeftViewWidth:option.leftViewWidth];
[self _changeRightViewWidth:option.rightViewWidth];
[self addSlideViewWidthObserver];
}

- (void)addSlideViewWidthObserver
{
[options addObserver:self
forKeyPath:NSStringFromSelector(@selector(leftViewWidth))
options:NSKeyValueObservingOptionNew
context:SlideViewObserverContext];
[options addObserver:self
forKeyPath:NSStringFromSelector(@selector(rightViewWidth))
options:NSKeyValueObservingOptionNew
context:SlideViewObserverContext];
}

- (void)removeSlideViewWidthObserver
{
@try {
[options removeObserver:self forKeyPath:NSStringFromSelector(@selector(leftViewWidth)) context:SlideViewObserverContext];
[options removeObserver:self forKeyPath:NSStringFromSelector(@selector(rightViewWidth)) context:SlideViewObserverContext];
}
@catch (NSException *exception) {

}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if (context == SlideViewObserverContext) {
if (object == options) {
if ([keyPath isEqualToString:NSStringFromSelector(@selector(leftViewWidth))]) {
[self handleLeftViewChange:change];
return;
}

if ([keyPath isEqualToString:NSStringFromSelector(@selector(rightViewWidth))]) {
[self handleRightViewChange:change];
return;
}
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

- (void)handleLeftViewChange:(NSDictionary<NSString *, id> *)change
{
NSNumber *newWidth = [change objectForKey:NSKeyValueChangeNewKey];

[self _changeLeftViewWidth:newWidth.floatValue];

}

- (void)handleRightViewChange:(NSDictionary<NSString *,id> *)change
{
NSNumber *newWidth = [change objectForKey:NSKeyValueChangeNewKey];

[self _changeRightViewWidth:newWidth.floatValue];

}

//- (void)changeLeftViewFrameWithWidth:(CGFloat)width
//{
// CGRect frame = _leftContainerView.frame;
// frame.origin.x = [self leftMinOrigin];
// frame.size.width = width;
// _leftContainerView.frame = frame;
//}
//
//- (void)changeRightViewFrameWithWidth:(CGFloat)width
//{
// CGRect frame = _rightContainerView.frame;
// frame.origin.x = [self rightMinOrigin];
// frame.size.width = width;
// _rightContainerView.frame = frame;
//}

//-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
Expand Down Expand Up @@ -591,6 +675,7 @@ -(void)closeLeftWithVelocity:(CGFloat) velocity {
CGFloat finalXOrigin = [self leftMinOrigin];

CGRect frame = _leftContainerView.frame;

frame.origin.x = finalXOrigin;

NSTimeInterval duration = options.animationDuration;
Expand Down Expand Up @@ -694,6 +779,9 @@ -(void)changeMainViewController:(UIViewController *)newMainController close:(BOO

-(void)changeLeftViewWidth:(CGFloat)width {
options.leftViewWidth = width;
}

- (void)_changeLeftViewWidth:(CGFloat)width {
CGRect leftFrame = self.view.bounds;
leftFrame.size.width = width;
leftFrame.origin.x = [self leftMinOrigin];
Expand All @@ -704,14 +792,17 @@ -(void)changeLeftViewWidth:(CGFloat)width {
}

-(void)changeRightViewWidth:(CGFloat)width {
options.rightBezelWidth = width;
options.rightViewWidth = width;
}

- (void)_changeRightViewWidth:(CGFloat)width {
CGRect rightFrame = self.view.bounds;
rightFrame.size.width = width;
rightFrame.origin.x = [self rightMinOrigin];
CGFloat rightOffset = 0;
rightFrame.origin.y = rightFrame.origin.y + rightOffset;
rightFrame.size.height = rightFrame.size.height - rightOffset;
_leftContainerView.frame = rightFrame;
_rightContainerView.frame = rightFrame;
}

-(void)changeLeftViewController:(UIViewController *)newLeftController close:(BOOL) close {
Expand Down Expand Up @@ -1061,4 +1152,4 @@ -(void)addPriorityToMenuGesture:(UIScrollView *) targetScrollView {
}
}

@end
@end