博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iphone开发之设备方向和角度计算
阅读量:6705 次
发布时间:2019-06-25

本文共 3221 字,大约阅读时间需要 10 分钟。

没什么好说的代码如下

 

头文件

 

/* 

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,如需转载请自行联系原作者
你可能感兴趣的文章
traceroute工作原理
查看>>
C++入门知识总结(1)
查看>>
IntegrityError duplicate key value violates unique constraint - django/postgres
查看>>
Android -- 自定义View小Demo,关于Rect绘制Android机器人(一)
查看>>
【Todo】Nodejs学习计划
查看>>
快速缓存接口开发
查看>>
SQLAlchemy ORM高级查询之过滤,排序
查看>>
Tomcat源码解读系列(一)——server.xml文件的配置
查看>>
进程的创建与可执行程序的加载
查看>>
大商创 sql追踪 卖家入驻
查看>>
CSS中如何把Span标签设置为固定宽度
查看>>
android设计的布局在阿拉伯语下界面错乱的解决方法
查看>>
HDU 1253 胜利大逃亡 NYOJ 523【BFS】
查看>>
VIM 插入
查看>>
iOS 开发如何入门
查看>>
关于Unity中的Input输入事件
查看>>
(转)Openlayers 2.X加载高德地图
查看>>
项目实战:如何构建知识图谱
查看>>
JVM Specification 9th Edition (2) Chapter 1. Introduction
查看>>
Linux开机启动脚本 - 转
查看>>