Skip to content

Commit

Permalink
Support autorotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
modocache committed Feb 25, 2013
1 parent 9aadb62 commit 2b226a5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
60 changes: 57 additions & 3 deletions MDCFocusView/MDCFocusView.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,21 @@ - (id)initWithFrame:(CGRect)frame {
self.userInteractionEnabled = NO;
self.opaque = NO;
self.alpha = 0.0f;

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onApplicationDidChangeStatusBarOrientationNotification:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
}
return self;
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
}


#pragma mark - UIView Overrides

Expand Down Expand Up @@ -93,15 +104,16 @@ - (void)focus:(UIView *)views, ... {
- (void)focusOnViews:(NSArray *)views {
NSParameterAssert(views != nil);

[self adjustRotation];
self.focused = YES;

NSMutableArray *focii = [NSMutableArray arrayWithCapacity:[views count]];

UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
for (UIView *view in views) {
MDCFocalPointView *focalPointView = [[self.focalPointViewClass alloc] initWithFocalView:view];
[self addSubview:focalPointView];
focalPointView.frame = [keyWindow convertRect:focalPointView.frame fromView:focalPointView.focalView.superview];
focalPointView.frame = [self convertRect:focalPointView.frame
fromView:focalPointView.focalView.superview];

[focii addObject:focalPointView];
}
Expand All @@ -117,7 +129,7 @@ - (void)focusOnViews:(NSArray *)views {
}

- (void)dismiss:(void (^)())completion {
NSAssert(self.focused, @"Cannot dismiss when focus is not applied in the first place.");
NSAssert(self.isFocused, @"Cannot dismiss when focus is not applied in the first place.");

[UIView animateWithDuration:self.focusDuration animations:^{
self.alpha = 0.0f;
Expand All @@ -137,4 +149,46 @@ - (void)dismiss:(void (^)())completion {
}];
}


#pragma mark - Internal Methods

- (void)onApplicationDidChangeStatusBarOrientationNotification:(NSNotification *)notification {
if (!self.isFocused) {
return;
}

NSMutableArray *views = [NSMutableArray new];
for (MDCFocalPointView *focalPointView in self.focii) {
[views addObject:focalPointView.focalView];
}

[self dismiss:^{
[self adjustRotation];
[self focusOnViews:[views copy]];
}];
}

- (void)adjustRotation {
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

CGFloat rotationAngle = 0.0f;
switch (orientation) {
case UIInterfaceOrientationPortrait:
rotationAngle = 0.0f;
break;
case UIInterfaceOrientationPortraitUpsideDown:
rotationAngle = M_PI;
break;
case UIInterfaceOrientationLandscapeLeft:
rotationAngle = -M_PI/2.0f;
break;
case UIInterfaceOrientationLandscapeRight:
rotationAngle = M_PI/2.0f;
break;
}

self.transform = CGAffineTransformMakeRotation(rotationAngle);
self.frame = self.superview.frame;
}

@end
2 changes: 2 additions & 0 deletions SampleApp/Controllers/SpotlightViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ - (void)viewDidAppear:(BOOL)animated {
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

if (self.focusView.isFocused) {
[self.focusView dismiss:^{
[self.focusView removeFromSuperview];
Expand Down
3 changes: 3 additions & 0 deletions SampleApp/SampleApp-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>

0 comments on commit 2b226a5

Please sign in to comment.