diff --git a/Source/SlideMenuController.m b/Source/SlideMenuController.m index a90b444..4349886 100644 --- a/Source/SlideMenuController.m +++ b/Source/SlideMenuController.m @@ -61,6 +61,7 @@ -(instancetype)init { static BOOL RPSWasOpenAtStartOfPan = NO; static BOOL RPSWasHiddenAtStartOfPan = NO; static UIGestureRecognizerState RPSLastState = UIGestureRecognizerStateEnded; +static void *SlideViewObserverContext = @"SlideMenuOptionsSlideViewContext"; @interface SlideMenuController () { @@ -79,6 +80,7 @@ -(instancetype)init { _leftContainerView = [UIView new]; _rightContainerView = [UIView new]; options = [[SlideMenuOption alloc] init]; + [self addSlideViewWidthObserver]; } return self; } @@ -122,6 +124,10 @@ -(instancetype)initWithMainViewController:(UIViewController *)tMainController le return self; } +- (void)dealloc { + [self removeSlideViewWidthObserver]; +} + -(void)awakeFromNib { [self initView]; } @@ -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 *)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 *)change +{ + NSNumber *newWidth = [change objectForKey:NSKeyValueChangeNewKey]; + + [self _changeLeftViewWidth:newWidth.floatValue]; + +} + +- (void)handleRightViewChange:(NSDictionary *)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]; @@ -591,6 +675,7 @@ -(void)closeLeftWithVelocity:(CGFloat) velocity { CGFloat finalXOrigin = [self leftMinOrigin]; CGRect frame = _leftContainerView.frame; + frame.origin.x = finalXOrigin; NSTimeInterval duration = options.animationDuration; @@ -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]; @@ -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 { @@ -1061,4 +1152,4 @@ -(void)addPriorityToMenuGesture:(UIScrollView *) targetScrollView { } } -@end +@end \ No newline at end of file