Поверните UIImageView В зависимости от iPhone Orientation

То, как я сделал бы это, все, что я хочу сделать, поворачивают a UIImageView в зависимости от ориентации iPhone.

8
задан Joshua 31 July 2010 в 10:46
поделиться

1 ответ

Вы можете сделать это через IB, чтобы получить приложение с портретным и ландшафтным расположением, или вы можете сделать это программно. Речь идет о программном способе.

Чтобы получить уведомления о смене ориентации, используйте

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(orientationChanged)
                name:UIDeviceOrientationDidChangeNotification
                object:nil];

и добавьте функцию, подобную этой (обратите внимание, что это копипаст из проекта и многие строки опущены, вам нужно будет настроить преобразование под вашу конкретную ситуацию)

-(void)orientationChanged
{
    UIDeviceOrientation o = [UIDevice currentDevice].orientation;

    CGFloat angle = 0;
    if ( o == UIDeviceOrientationLandscapeLeft ) angle = M_PI_2;
    else if ( o == UIDeviceOrientationLandscapeRight ) angle = -M_PI_2;
    else if ( o == UIDeviceOrientationPortraitUpsideDown ) angle = M_PI;

    [UIView beginAnimations:@"rotate" context:nil];
    [UIView setAnimationDuration:0.7];
    self.rotateView.transform = CGAffineTransformRotate(
                                CGAffineTransformMakeTranslation(
                                    160.0f-self.rotateView.center.x,
                                    240.0f-self.rotateView.center.y
                                ),angle);
    [UIView commitAnimations];
}

Когда вы закончите, остановите уведомления так:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self];
18
ответ дан 5 December 2019 в 09:23
поделиться