Orientation - 2
来源:互联网 发布:java生成验证码工具类 编辑:程序博客网 时间:2024/06/10 02:24
先来看看很多的一个总结:http://www.cnblogs.com/tekkaman/p/3679848.html
在项目中遇到一个非常诡异的问题(诡异都是相对的,相对于我没有这块知识的前提下决定诡异哈哈)
就是我的悬浮框再手机“即将转换方向”的时候出现了错位的问题,而这个“即将转换方法”的动作也只是讲手机前倾或者后仰(了解了这块知识之后发现iPhone 的sensor度也太高了吧!)
这段代码的大概:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];#pragma mark - UIDeviceOrientationDidChangeNotification- (void)handleOrientationDidChange:(NSNotification *)notification{ if (self.isYinLian) { return; } UIWindow *keyWindow = GetKeyWindow; if (_flowButton.isSelected) { if (_flowButton.center.x >= VIEW_W(keyWindow) * 0.5f) { LOG(@"a"); CGPoint v = CGPointMake( -(VIEW_W(_flowButton) * 0.5f + VIEW_W(_flowMenu)*0.5f - 13), 0); LOG(@"%f, %f", _flowButton.center.x, _flowButton.center.y); LOG(@"%f", keyWindow.bounds.size.width); _flowMenu.center = cgpAdd(_flowButton.center, v); _flowMenu.hidden = NO; } else { LOG(@"a a"); CGPoint v = CGPointMake( VIEW_W(_flowButton)*0.5f + VIEW_W(_flowMenu)*0.5f - 13, 0); _flowMenu.center = cgpAdd(_flowButton.center, v); _flowMenu.hidden = NO; } } if (_isFlowButtonDismiss) { if (_flowButton.center.x >= VIEW_W(keyWindow) * 0.5f) { _flowButton.frame = CGRectMake(_flowButton.superview.bounds.size.width - 10, _flowButton.frame.origin.y, _flowButton.bounds.size.width, _flowButton.bounds.size.height); LOG(@"b"); } else { _flowButton.frame = CGRectMake(-_flowButton.bounds.size.width + 10, _flowButton.frame.origin.y, _flowButton.bounds.size.width, _flowButton.bounds.size.height); LOG(@"b b"); } }}
原来错误就是这里设置的坐标有误!!
我们来看看这方面的API。
@property(nonatomic,readonly,getter=isGeneratingDeviceOrientationNotifications) BOOL generatesDeviceOrientationNotifications __TVOS_PROHIBITED;- (void)beginGeneratingDeviceOrientationNotifications __TVOS_PROHIBITED; // nestable- (void)endGeneratingDeviceOrientationNotifications __TVOS_PROHIBITED;
UIKIT_EXTERN NSString *const UIDeviceOrientationDidChangeNotification __TVOS_PROHIBITED;
原来是系统的一个关于屏幕方向的通知。
对于iPad这种屏幕方向经常变动的设备,这真是神器啊!
-(void)viewDidLoad{ [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotateViewInterfaceOrientation) name:UIDeviceOrientationDidChangeNotification object:nil];}- (void)rotateViewInterfaceOrientation { UIInterfaceOrientation tmpOri =self.interfaceOrientation; if (UIDeviceOrientationIsPortrait(tmpOri)) { //self.view.frame=CGRectMake(0, 0, 1004, 768); } else { //self.view.frame=CGRectMake(0, 0, 768, 1004); }}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ if (UIDeviceOrientationIsPortrait(interfaceOrientation)) { //self.view.frame=CGRectMake(0, 0, 1004, 768); } else { //self.view.frame=CGRectMake(0, 0, 768, 1004); } return YES;}
还可以在触发动作里面设置:
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];if (orientation == UIDeviceOrientationPortrait) [self doSomething];else [self doSomethingElse];
至于如果需要关闭通知(某些情况,例如视图控制器消失viewWillDisappear等)系统提供了一个相应的关闭通知的方法:
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
也可以用手动关闭通知的方法:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
参考:
http://blog.csdn.net/daneildemon/article/details/6102296
http://stackoverflow.com/questions/15996646/begingeneratingdeviceorientationnotifications-how-to-work
http://stackoverflow.com/questions/3361166/uideviceorientationdidchangenotification-wont-stop
0 0
- Orientation - 2
- orientation
- Project Management Orientation (Concepts -2)
- ios 屏幕旋转Orientation总结2
- JPEG Orientation
- About Orientation
- camera orientation
- orientation|keyboardHidden
- Orientation - 1
- android:orientation
- Orientation - 3
- Interface Orientation
- Device orientation vs interface orientation
- 关于Wrong orientation? No orientation specified问题
- A Fingerprint Orientation Model Based on 2D Fourier expansion (FOMFE) and Its Application
- Readings in Service Orientation
- Orientation Histogram code
- WebKit General Orientation
- 课程设计-学生成绩系统
- 冒泡排序改进版--添加标示排序法
- mysql has gone away的原因
- 【Linux】ubuntu下安装MySQL、Redis
- 数据结构C语言版进制转换
- Orientation - 2
- XJ001班2015年12月24日课堂总结与作业
- 慕课网-Java入门第二季实战练习-答答租车系统
- Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)
- Linux Kernel 定时器和时间管理(浅析)
- leetcode -- Maximum Gap -- 与distributed sorting有关,重点复习一下
- 《Android群英传》读书笔记(11)第十章:Android性能优化
- 创建简单的数据库的表和表的连接
- 数据结构C语言版之括号匹配