iphone开发之设备方向和角度计算

简介:

没什么好说的代码如下

 

头文件////////////

 

/* 
Erica Sadun, http://ericasadun.com 
iPhone Developer's Cookbook, 3.0 Edition 
BSD License, Use at your own risk 
*/ 

#import <UIKit/UIKit.h> 


@interface UIDevice (Orientation) <UIAccelerometerDelegate> 
- (BOOL) isLandscape; 
- (BOOL) isPortrait; 
- (NSString *) orientationString; 
- (float) orientationAngleRelativeToOrientation:(UIDeviceOrientation) someOrientation; 
@property (nonatomic, readonly) BOOL isLandscape; 
@property (nonatomic, readonly) BOOL isPortrait; 
@property (nonatomic, readonly) NSString *orientationString; 
@property (nonatomic, readonly) float orientationAngle; 
@end

 

实现文件///////////

 

/* 
Erica Sadun, http://ericasadun.com 
iPhone Developer's Cookbook, 3.0 Edition 
BSD License, Use at your own risk 
*/ 

// Thanks jweinberg, Emanuele Vulcano, rincewind42, Jonah Williams 

#import "UIDevice-Orientation.h" 

@implementation UIDevice (Orientation) 

float device_angle; 
CFRunLoopRef currentLoop; 

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 

    float xx = [acceleration x]; 
    float yy = -[acceleration y]; 
    device_angle = M_PI / 2.0f - atan2(yy, xx); 
    
    CFRunLoopStop(currentLoop); 


- (float) orientationAngle 

#if TARGET_IPHONE_SIMULATOR 
    // NSLog( @"Running in the simulator!" ); 
    switch (self.orientation) 
    { 
        case UIDeviceOrientationPortrait: 
            return 0.0f; 
        case UIDeviceOrientationPortraitUpsideDown: 
            return M_PI; 
        case UIDeviceOrientationLandscapeLeft: 
            return -(M_PI/2.0f); 
        case UIDeviceOrientationLandscapeRight: 
            return (M_PI/2.0f); 
        default: 
            return 0.0f; 
    } 
#else 
    id accelerometer_delegate = [UIAccelerometer sharedAccelerometer].delegate; 
    [UIAccelerometer sharedAccelerometer].delegate = self; 
    
    // Wait for a reading 
    currentLoop = CFRunLoopGetCurrent(); 
    CFRunLoopRun(); 
    [UIAccelerometer sharedAccelerometer].delegate = accelerometer_delegate; 
    
    return device_angle; 
#endif 


// Thanks Jonah Williams 
- (float) orientationAngleRelativeToOrientation:(UIDeviceOrientation) someOrientation 

    float dOrientation = 0.0f; 
    switch (someOrientation) 
    { 
        case UIDeviceOrientationPortraitUpsideDown: {dOrientation = M_PI; break;} 
        case UIDeviceOrientationLandscapeLeft: {dOrientation = -(M_PI/2.0f); break;} 
        case UIDeviceOrientationLandscapeRight: {dOrientation = (M_PI/2.0f); break;} 
        default: break; 
    } 
    
    float adjustedAngle = fmod(self.orientationAngle - dOrientation, 2.0f * M_PI); 
    if (adjustedAngle &gt; (M_PI + 0.01f)) adjustedAngle = (adjustedAngle - 2.0f * M_PI); 
    return adjustedAngle; 


- (BOOL) isLandscape 

    return UIDeviceOrientationIsLandscape(self.orientation); 


- (BOOL) isPortrait 

    return UIDeviceOrientationIsPortrait(self.orientation); 


- (NSString *) orientationString 

    switch ([[UIDevice currentDevice] orientation]) 
    { 
        case UIDeviceOrientationUnknown: return @"Unknown"; 
        case UIDeviceOrientationPortrait: return @"Portrait"; 
        case UIDeviceOrientationPortraitUpsideDown: return @"Portrait Upside Down"; 
        case UIDeviceOrientationLandscapeLeft: return @"Landscape Left"; 
        case UIDeviceOrientationLandscapeRight: return @"Landscape Right"; 
        case UIDeviceOrientationFaceUp: return @"Face Up"; 
        case UIDeviceOrientationFaceDown: return @"Face Down"; 
        default: break; 
    } 
    return nil; 

@end










本文转自 arthurchen 51CTO博客,原文链接:http://blog.51cto.com/arthurchen/575684,如需转载请自行联系原作者
目录
相关文章
|
Web App开发 网络虚拟化 iOS开发
如何获取苹果设备的UDID(iPhone/iPad UDID查询方法)
如何获取苹果设备的UDID(iPhone/iPad UDID查询方法)
|
编解码 iOS开发
iphone 开发的基本入门知识
iphone 开发的基本入门知识
156 0
「镁客早报」iPhone或将在今年采用三摄;传Facebook致力于开发语音助力服务与亚马逊、苹果竞争
亚马逊向美国Alexa设备推免费音乐服务;视频会议软件开发商Zoom纳斯达克上市。
229 0
|
Web App开发 缓存 开发工具
|
存储 iOS开发 计算机视觉
|
Web App开发 前端开发 JavaScript
|
iOS开发
关于iPhone设备不同显示尺寸适配的一些方法
关于iPhone设备不同显示尺寸适配的一些方法     ------关于适配的理解------ 1、什么是适配?     适配是对不同硬件和系统软件的适应,硬件包括屏幕显示,处理器,内存等等(目前主要是屏幕适配,iPhone 6s新增了3D Touch功能,其他硬件功能并没有实质性的改变),软件主要是不同iOS系统(有些在最新系统(通常会尽量向下兼容)不再支持的方法都需要做一些判断)。
856 0