简体   繁体   中英

CATransform3DMakeRotation only seems to work right the second time

i'm using the following pretty basic code as I'm trying to learn how to do transformations with a basic landscape rotation for beginners

I wanted to do it the long way to learn more than just throwing in the 'shouldautorotate' stuff, this is the code I'm working off of now

- (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) {

        }];
    }
}

however the first time I rotate from portrait, I get this

肖像景观

but once I return it to portrait and rotate again I receieve 更正确

I really apologize if this is a stupid question or I'm missing something obvious. I'm learning on my own generally with the help of SO :)

Thanks for any help, have a good one guys.

if you change the centre or frame on Portrait, you should change it back on Landscape... also you should change the contentSize of your scroll view every time it is rotated.

try something like this...

- (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) {

        }];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM