Orientation - 2

先来看看很多的一个总结: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值