iOS监听屏幕旋转的几种方法

//通过控制器得到屏幕旋转状态

//获取将要旋转的状态
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [superwillRotateToInterfaceOrientation:toInterfaceOrientationduration:duration];
   
    [self.ImageBrowsingViewgetIndexPath];
}

//获取旋转中的状态
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [superwillAnimateRotationToInterfaceOrientation:toInterfaceOrientationduration:duration];
   
    [self.ImageBrowsingViewcollectionReload];
}
//屏幕旋转完成的状态
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientationNS_DEPRECATED_IOS(2_0,8_0)__TVOS_PROHIBITED
{
     
}

//通过通知监听屏幕旋转
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(OrientationDidChange:)name:UIDeviceOrientationDidChangeNotificationobject:nil];

如果想做横屏效果,特别是图片浏览,那么就应该考虑上一次屏幕处于什么状态,将要改变的size是多少

static UIInterfaceOrientation lastType = UIInterfaceOrientationPortrait;
//获取将要旋转的状态
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    
    if ((toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) )
    {
        
        if ((lastType == UIInterfaceOrientationLandscapeLeft) || (lastType == UIInterfaceOrientationLandscapeRight))
        {
            [self.ImageBrowsingView collectionReload];
        }
    }
    else
    {
        
        if ((lastType == UIInterfaceOrientationPortrait) || (lastType == UIInterfaceOrientationPortraitUpsideDown))
        {
            [self.ImageBrowsingView collectionReload];
        }
    }
    
    
}


你可能感兴趣的:(iOS---小小总结,ios,屏幕旋转)