1

我正在使用以下非常基本的代码,因为我正在尝试学习如何为初学者使用基本的景观旋转进行转换

我想学习更多的东西,而不仅仅是投入“shouldautorotate”的东西,这是我现在正在工作的代码

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
}


- (void)didRotate:(NSNotification *)notification
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
    if (orientation == UIDeviceOrientationLandscapeRight)
    { 
        [UIView animateWithDuration:0.2f animations:^{
            self.view.layer.transform = CATransform3DMakeRotation(-M_PI/2, 0, 0.0, 1.0); 
            [self.scrollViewImages setContentSize:CGSizeMake(self.view.frame.size.width*self.images.count, 320.0)];
            [self.scrollViewImages setContentOffset:CGPointMake(0, 0) animated:YES];
        } completion:^(BOOL finished) {

        }];
    } else if(orientation == UIDeviceOrientationPortrait) {
        [UIView animateWithDuration:0.2f animations:^{ 
            self.view.frame = CGRectMake(0, 0, 320.0, 480.0);
            self.view.center = CGPointMake(160.0, 240.0);
            self.view.transform = CGAffineTransformIdentity;            
        } completion:^(BOOL finished) {

        }];
    }
}

但是我第一次从肖像旋转时,我得到了这个

肖像 景观

但是一旦我将其恢复为纵向并再次旋转,我就会收到 更正确一点

如果这是一个愚蠢的问题,或者我遗漏了一些明显的东西,我真的很抱歉。我通常在 SO 的帮助下自己学习 :)

感谢您的帮助,祝大家好运。

4

1 回答 1

0

如果您更改纵向上的中心或框架,则应将其更改回横向...您还应在每次旋转时更改滚动视图的 contentSize。

尝试这样的事情......

- (void)didRotate:(NSNotification *)notification
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
    if (orientation == UIDeviceOrientationLandscapeRight)
    { 
        [UIView animateWithDuration:0.2f animations:^{
            self.view.frame = CGRectMake(0, 0, 480.0, 320.0);
            self.view.center = CGPointMake(240.0, 160.0);

            self.view.layer.transform = CATransform3DMakeRotation(-M_PI/2, 0, 0.0, 1.0); 
            [self.scrollViewImages setContentSize:CGSizeMake(self.view.frame.size.width*self.images.count, 320.0)];
            [self.scrollViewImages setContentOffset:CGPointMake(0, 0) animated:YES];
        } completion:^(BOOL finished) {

        }];
    } else if(orientation == UIDeviceOrientationPortrait) {
        [UIView animateWithDuration:0.2f animations:^{ 
            self.view.frame = CGRectMake(0, 0, 320.0, 480.0);
            self.view.center = CGPointMake(160.0, 240.0);
            self.view.transform = CGAffineTransformIdentity;            

            [self.scrollViewImages setContentSize:CGSizeMake(self.view.frame.size.width*self.images.count, 320.0)];
            [self.scrollViewImages setContentOffset:CGPointMake(0, 0) animated:YES];

        } completion:^(BOOL finished) {

        }];
    }
}
于 2013-02-13T03:25:51.457 回答