Skip to content

Commit 1866b5a

Browse files
author
Mike Enriquez + Leon Gersing
committed
Post notifications for top view positions
- Removed performing selectors on child views. Notifications can happen asynchronously.
1 parent 27116f7 commit 1866b5a

File tree

3 files changed

+78
-18
lines changed

3 files changed

+78
-18
lines changed

ECSlidingViewController/ThirdTopViewController.m

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,32 @@
1010

1111
@implementation ThirdTopViewController
1212

13+
- (void)awakeFromNib
14+
{
15+
[[NSNotificationCenter defaultCenter] addObserver:self
16+
selector:@selector(underLeftWillAppear:)
17+
name:ECSlidingViewUnderLeftWillAppear
18+
object:self.slidingViewController];
19+
[[NSNotificationCenter defaultCenter] addObserver:self
20+
selector:@selector(topDidAnchorRight:)
21+
name:ECSlidingViewTopDidAnchorRight
22+
object:self.slidingViewController];
23+
24+
[[NSNotificationCenter defaultCenter] addObserver:self
25+
selector:@selector(underRightWillAppear:)
26+
name:ECSlidingViewUnderRightWillAppear
27+
object:self.slidingViewController];
28+
[[NSNotificationCenter defaultCenter] addObserver:self
29+
selector:@selector(topDidAnchorLeft:)
30+
name:ECSlidingViewTopDidAnchorLeft
31+
object:self.slidingViewController];
32+
33+
[[NSNotificationCenter defaultCenter] addObserver:self
34+
selector:@selector(topDidReset:)
35+
name:ECSlidingViewTopDidReset
36+
object:self.slidingViewController];
37+
}
38+
1339
- (void)viewWillAppear:(BOOL)animated
1440
{
1541
[super viewWillAppear:animated];
@@ -30,18 +56,28 @@ - (IBAction)revealMenu:(id)sender
3056
[self.slidingViewController anchorTopViewTo:ECRight animations:nil onComplete:nil];
3157
}
3258

33-
// callbacks from slidingViewController
34-
- (void)underLeftWillAppear
59+
// slidingViewController notification
60+
- (void)underLeftWillAppear:(NSNotification *)notification
3561
{
3662
NSLog(@"under left will appear");
3763
}
3864

39-
- (void)underRightWillAppear
65+
- (void)topDidAnchorRight:(NSNotification *)notification
66+
{
67+
NSLog(@"top did anchor right");
68+
}
69+
70+
- (void)underRightWillAppear:(NSNotification *)notification
4071
{
4172
NSLog(@"under right will appear");
4273
}
4374

44-
- (void)topDidReset
75+
- (void)topDidAnchorLeft:(NSNotification *)notification
76+
{
77+
NSLog(@"top did anchor left");
78+
}
79+
80+
- (void)topDidReset:(NSNotification *)notification
4581
{
4682
NSLog(@"top did reset");
4783
}

ECSlidingViewController/Vendor/ECSlidingViewController/ECSlidingViewController.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@
99
#import <UIKit/UIKit.h>
1010
#import "UIImage+ImageWithUIView.h"
1111

12+
/** Notification that gets posted when the underRight view will appear */
13+
extern NSString *const ECSlidingViewUnderRightWillAppear;
14+
15+
/** Notification that gets posted when the underLeft view will appear */
16+
extern NSString *const ECSlidingViewUnderLeftWillAppear;
17+
18+
/** Notification that gets posted when the top view is anchored to the left side of the screen */
19+
extern NSString *const ECSlidingViewTopDidAnchorLeft;
20+
21+
/** Notification that gets posted when the top view is anchored to the right side of the screen */
22+
extern NSString *const ECSlidingViewTopDidAnchorRight;
23+
24+
/** Notification that gets posted when the top view is centered on the screen */
25+
extern NSString *const ECSlidingViewTopDidReset;
26+
1227
/** @constant ECSide side of screen */
1328
typedef enum {
1429
/** Left side of screen */

ECSlidingViewController/Vendor/ECSlidingViewController/ECSlidingViewController.m

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
#import "ECSlidingViewController.h"
1010

11+
NSString *const ECSlidingViewUnderRightWillAppear = @"ECSlidingViewUnderRightWillAppear";
12+
NSString *const ECSlidingViewUnderLeftWillAppear = @"ECSlidingViewUnderLeftWillAppear";
13+
NSString *const ECSlidingViewTopDidAnchorLeft = @"ECSlidingViewTopDidAnchorLeft";
14+
NSString *const ECSlidingViewTopDidAnchorRight = @"ECSlidingViewTopDidAnchorRight";
15+
NSString *const ECSlidingViewTopDidReset = @"ECSlidingViewTopDidReset";
16+
1117
@interface ECSlidingViewController()
1218

1319
@property (nonatomic, strong) UIView *topViewSnapshot;
@@ -35,7 +41,6 @@ - (void)underLeftWillAppear;
3541
- (void)underRightWillAppear;
3642
- (void)topDidReset;
3743
- (BOOL)topViewHasFocus;
38-
- (void)performSelectorOnChildViewControllers:(SEL)selector;
3944

4045
@end
4146

@@ -222,6 +227,10 @@ - (void)anchorTopViewTo:(ECSide)side animations:(void (^)())animations onComplet
222227
}
223228

224229
[self addTopViewSnapshot];
230+
dispatch_async(dispatch_get_main_queue(), ^{
231+
NSString *key = (side == ECLeft) ? ECSlidingViewTopDidAnchorLeft : ECSlidingViewTopDidAnchorRight;
232+
[[NSNotificationCenter defaultCenter] postNotificationName:key object:self userInfo:nil];
233+
});
225234
}];
226235
}
227236

@@ -247,6 +256,10 @@ - (void)anchorTopViewOffScreenTo:(ECSide)side animations:(void(^)())animations o
247256
complete();
248257
}
249258
[self addTopViewSnapshot];
259+
dispatch_async(dispatch_get_main_queue(), ^{
260+
NSString *key = (side == ECLeft) ? ECSlidingViewTopDidAnchorLeft : ECSlidingViewTopDidAnchorRight;
261+
[[NSNotificationCenter defaultCenter] postNotificationName:key object:self userInfo:nil];
262+
});
250263
}];
251264
}
252265

@@ -391,23 +404,29 @@ - (CGFloat)screenWidthForOrientation:(UIInterfaceOrientation)orientation
391404

392405
- (void)underLeftWillAppear
393406
{
394-
[self performSelectorOnChildViewControllers:@selector(underLeftWillAppear)];
407+
dispatch_async(dispatch_get_main_queue(), ^{
408+
[[NSNotificationCenter defaultCenter] postNotificationName:ECSlidingViewUnderLeftWillAppear object:self userInfo:nil];
409+
});
395410
self.underRightView.hidden = YES;
396411
[self.underLeftViewController viewWillAppear:NO];
397412
self.underLeftView.hidden = NO;
398413
}
399414

400415
- (void)underRightWillAppear
401416
{
402-
[self performSelectorOnChildViewControllers:@selector(underRightWillAppear)];
417+
dispatch_async(dispatch_get_main_queue(), ^{
418+
[[NSNotificationCenter defaultCenter] postNotificationName:ECSlidingViewUnderRightWillAppear object:self userInfo:nil];
419+
});
403420
self.underLeftView.hidden = YES;
404421
[self.underRightViewController viewWillAppear:NO];
405422
self.underRightView.hidden = NO;
406423
}
407424

408425
- (void)topDidReset
409426
{
410-
[self performSelectorOnChildViewControllers:@selector(topDidReset)];
427+
dispatch_async(dispatch_get_main_queue(), ^{
428+
[[NSNotificationCenter defaultCenter] postNotificationName:ECSlidingViewTopDidReset object:self userInfo:nil];
429+
});
411430
[self.topView removeGestureRecognizer:self.resetTapGesture];
412431
[self removeTopViewSnapshot];
413432
self.panGesture.enabled = YES;
@@ -418,14 +437,4 @@ - (BOOL)topViewHasFocus
418437
return self.topView.center.x == self.resettedCenter;
419438
}
420439

421-
- (void)performSelectorOnChildViewControllers:(SEL)selector
422-
{
423-
NSArray *childViewControllers = [self childViewControllers];
424-
for (UIViewController *childViewController in childViewControllers) {
425-
if ([childViewController respondsToSelector:selector]) {
426-
[childViewController performSelector:selector];
427-
}
428-
}
429-
}
430-
431440
@end

0 commit comments

Comments
 (0)