Skip to content

Commit

Permalink
enhance getting an orientation logic and demo program
Browse files Browse the repository at this point in the history
  • Loading branch information
tastyone committed Apr 22, 2016
1 parent 4c43bc0 commit f34ce4c
Show file tree
Hide file tree
Showing 9 changed files with 422 additions and 519 deletions.
15 changes: 11 additions & 4 deletions Demo/Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
2CA760291CCA00F900B538F0 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2CA760281CCA00F900B538F0 /* ViewController.m */; };
E5E7A6E417C1F57A00803A0D /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E5E7A6E317C1F57A00803A0D /* UIKit.framework */; };
E5E7A6E617C1F57A00803A0D /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E5E7A6E517C1F57A00803A0D /* Foundation.framework */; };
E5E7A6E817C1F57A00803A0D /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E5E7A6E717C1F57A00803A0D /* CoreGraphics.framework */; };
Expand All @@ -22,6 +23,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
2CA760271CCA00F900B538F0 /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
2CA760281CCA00F900B538F0 /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
E5E7A6E017C1F57A00803A0D /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
E5E7A6E317C1F57A00803A0D /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
E5E7A6E517C1F57A00803A0D /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -90,6 +93,8 @@
children = (
E5E7A6F217C1F57A00803A0D /* AppDelegate.h */,
E5E7A6F317C1F57A00803A0D /* AppDelegate.m */,
2CA760271CCA00F900B538F0 /* ViewController.h */,
2CA760281CCA00F900B538F0 /* ViewController.m */,
E5E7A6FE17C1F57A00803A0D /* ViewController.xib */,
E5E7A6EA17C1F57A00803A0D /* Supporting Files */,
);
Expand Down Expand Up @@ -187,6 +192,7 @@
files = (
E5E7A6F017C1F57A00803A0D /* main.m in Sources */,
E5E7A6F417C1F57A00803A0D /* AppDelegate.m in Sources */,
2CA760291CCA00F900B538F0 /* ViewController.m in Sources */,
E5E7A72817C1F59E00803A0D /* MotionOrientation.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -238,7 +244,7 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 6.1;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
};
Expand All @@ -262,7 +268,7 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 6.1;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
Expand All @@ -275,7 +281,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Demo/Demo-Prefix.pch";
INFOPLIST_FILE = "Demo/Demo-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -287,7 +293,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Demo/Demo-Prefix.pch";
INFOPLIST_FILE = "Demo/Demo-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -312,6 +318,7 @@
E5E7A70517C1F57A00803A0D /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
10 changes: 6 additions & 4 deletions Demo/Demo/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
@property (strong, nonatomic) UIWindow * window;

@property (strong, nonatomic) IBOutlet UIViewController * viewController;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel * label1;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel * label2;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel * label3;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel * label4;

@property (unsafe_unretained, nonatomic) IBOutlet UILabel * labelDebugData;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel * labelMotionDeviceOrientation;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel * labelMotionInterfaceOrientation;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel * labelDeviceOrientation;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel * labelInterfaceOrientation;

@end
75 changes: 68 additions & 7 deletions Demo/Demo/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

@implementation AppDelegate

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAll;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
Expand All @@ -21,34 +26,73 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[self.window makeKeyAndVisible];

// Register for MotionOrientation orientation changes
[MotionOrientation initialize];
[[MotionOrientation sharedInstance] startAccelerometerUpdates];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(accelerometerUpdated:)
name:MotionOrientationAccelerometerUpdatedNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(motionOrientationChanged:)
selector:@selector(motionDeviceOrientationChanged:)
name:MotionOrientationChangedNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(motionInterfaceOrientationChanged:)
name:MotionOrientationInterfaceOrientationChangedNotification
object:nil];

// Register for UIDevice orientation changes
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceOrientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(interfaceOrientationChanged:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];

return YES;
}

- (void)motionOrientationChanged:(NSNotification *)notification
- (void)accelerometerUpdated:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
_label1.text = notification.description;
_label2.text = [self stringDescriptionForDeviceOrientation:[MotionOrientation sharedInstance].deviceOrientation];
self.labelDebugData.text = [notification.userInfo valueForKey:kMotionOrientationDebugDataKey];
});
}

- (void)motionDeviceOrientationChanged:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
self.labelMotionDeviceOrientation.text = [NSString stringWithFormat:@"%@ %@",
[self stringDescriptionForDeviceOrientation:[MotionOrientation sharedInstance].deviceOrientation],
[notification.userInfo valueForKey:kMotionOrientationDebugDataKey]
];
});
}

- (void)deviceOrientationChanged:(NSNotification *)notification
{
_label3.text = notification.description;
_label4.text = [self stringDescriptionForDeviceOrientation:[UIDevice currentDevice].orientation];
dispatch_async(dispatch_get_main_queue(), ^{
self.labelDeviceOrientation.text = [self stringDescriptionForDeviceOrientation:[UIDevice currentDevice].orientation];
});
}

- (void)motionInterfaceOrientationChanged:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
self.labelMotionInterfaceOrientation.text = [NSString stringWithFormat:@"%@ %@",
[self stringDescriptionForInterfaceOrientation:[MotionOrientation sharedInstance].interfaceOrientation],
[notification.userInfo valueForKey:kMotionOrientationDebugDataKey]
];
});
}

- (void)interfaceOrientationChanged:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
self.labelInterfaceOrientation.text = [self stringDescriptionForInterfaceOrientation:self.viewController.interfaceOrientation];
});
}

- (NSString *)stringDescriptionForDeviceOrientation:(UIDeviceOrientation)orientation
Expand All @@ -73,5 +117,22 @@ - (NSString *)stringDescriptionForDeviceOrientation:(UIDeviceOrientation)orienta
}
}

- (NSString *)stringDescriptionForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
switch (orientation)
{
case UIInterfaceOrientationPortrait:
return @"Portrait";
case UIInterfaceOrientationPortraitUpsideDown:
return @"PortraitUpsideDown";
case UIInterfaceOrientationLandscapeLeft:
return @"LandscapeLeft";
case UIInterfaceOrientationLandscapeRight:
return @"LandscapeRight";
default:
return @"Unknown";
}
}

@end

1 change: 1 addition & 0 deletions Demo/Demo/Demo-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
</dict>
</plist>
13 changes: 13 additions & 0 deletions Demo/Demo/ViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ViewController.h
// Demo
//
// Created by Sangwon Park on 4/22/16.
//
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end
57 changes: 57 additions & 0 deletions Demo/Demo/ViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// ViewController.m
// Demo
//
// Created by Sangwon Park on 4/22/16.
//
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(BOOL)shouldAutorotate
{
return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
Loading

0 comments on commit f34ce4c

Please sign in to comment.