From e41b7a543d06eb9d94b420a58639948e48ce4411 Mon Sep 17 00:00:00 2001 From: lxyzk Date: Tue, 26 Jul 2016 11:52:57 +0800 Subject: [PATCH 1/2] fix left and right view width fix left and right view width --- Source/SlideMenuController.m | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Source/SlideMenuController.m b/Source/SlideMenuController.m index a90b444..15e6a92 100644 --- a/Source/SlideMenuController.m +++ b/Source/SlideMenuController.m @@ -79,6 +79,7 @@ -(instancetype)init { _leftContainerView = [UIView new]; _rightContainerView = [UIView new]; options = [[SlideMenuOption alloc] init]; + [options addObserver:self forKeyPath:@"leftViewWitdth" options:NSKeyValueObservingOptionNew context:nil]; } return self; } @@ -174,6 +175,28 @@ -(SlideMenuOption *)option { -(void)setOption:(SlideMenuOption *)option { options = option; + [options addObserver:self forKeyPath:@"leftViewWitdth" options:NSKeyValueObservingOptionNew context:nil]; +} + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + if (object == options) { + if ([keyPath isEqualToString:@"leftViewWitdth"]) { + CGRect leftFrame = _leftContainerView.frame; + NSNumber *newWidth = [change objectForKey:NSKeyValueChangeNewKey]; + leftFrame.size.width = [newWidth floatValue]; + _leftContainerView.frame = leftFrame; + return; + } + + if ([keyPath isEqualToString:@"rightViewWidth"]) { + CGRect rightFrame = _rightContainerView.frame; + NSNumber *newWidth = [change objectForKey:NSKeyValueChangeNewKey]; + rightFrame.size.width = [newWidth floatValue]; + _rightContainerView.frame = rightFrame; + return; + } + } } From 49af0fbcab81b2c5240e3421d1c5fa63ceed575a Mon Sep 17 00:00:00 2001 From: lxyzk Date: Wed, 27 Jul 2016 14:34:54 +0800 Subject: [PATCH 2/2] right view width --- Source/SlideMenuController.m | 108 ++++++++++++++++++++++++++++------- 1 file changed, 88 insertions(+), 20 deletions(-) diff --git a/Source/SlideMenuController.m b/Source/SlideMenuController.m index 15e6a92..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,7 +80,7 @@ -(instancetype)init { _leftContainerView = [UIView new]; _rightContainerView = [UIView new]; options = [[SlideMenuOption alloc] init]; - [options addObserver:self forKeyPath:@"leftViewWitdth" options:NSKeyValueObservingOptionNew context:nil]; + [self addSlideViewWidthObserver]; } return self; } @@ -123,6 +124,10 @@ -(instancetype)initWithMainViewController:(UIViewController *)tMainController le return self; } +- (void)dealloc { + [self removeSlideViewWidthObserver]; +} + -(void)awakeFromNib { [self initView]; } @@ -174,31 +179,87 @@ -(SlideMenuOption *)option { } -(void)setOption:(SlideMenuOption *)option { + [self removeSlideViewWidthObserver]; options = option; - [options addObserver:self forKeyPath:@"leftViewWitdth" options:NSKeyValueObservingOptionNew context:nil]; + [self _changeLeftViewWidth:option.leftViewWidth]; + [self _changeRightViewWidth:option.rightViewWidth]; + [self addSlideViewWidthObserver]; } -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +- (void)addSlideViewWidthObserver { - if (object == options) { - if ([keyPath isEqualToString:@"leftViewWitdth"]) { - CGRect leftFrame = _leftContainerView.frame; - NSNumber *newWidth = [change objectForKey:NSKeyValueChangeNewKey]; - leftFrame.size.width = [newWidth floatValue]; - _leftContainerView.frame = leftFrame; - return; - } + [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) { - if ([keyPath isEqualToString:@"rightViewWidth"]) { - CGRect rightFrame = _rightContainerView.frame; - NSNumber *newWidth = [change objectForKey:NSKeyValueChangeNewKey]; - rightFrame.size.width = [newWidth floatValue]; - _rightContainerView.frame = rightFrame; - return; + } +} + +- (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]; @@ -614,6 +675,7 @@ -(void)closeLeftWithVelocity:(CGFloat) velocity { CGFloat finalXOrigin = [self leftMinOrigin]; CGRect frame = _leftContainerView.frame; + frame.origin.x = finalXOrigin; NSTimeInterval duration = options.animationDuration; @@ -717,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]; @@ -727,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 { @@ -1084,4 +1152,4 @@ -(void)addPriorityToMenuGesture:(UIScrollView *) targetScrollView { } } -@end +@end \ No newline at end of file