shouldAutorotateToInterfaceOrientation不改变对象位置

时间:2012-07-17 13:06:45

标签: iphone ios xcode

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

if (interfaceOrientation == (UIInterfaceOrientationPortrait))
    [self embedYouTube:yout frame:CGRectMake(30, 155, 260, 200)];

if (interfaceOrientation == (UIInterfaceOrientationLandscapeRight))
    [self embedYouTube:yout frame:CGRectMake(30, 155, 400, 200)];

if (interfaceOrientation == (UIInterfaceOrientationLandscapeLeft))
    [self embedYouTube:yout frame:CGRectMake(30, 155, 400, 200)];


if (interfaceOrientation == (UIInterfaceOrientationPortraitUpsideDown))
    [self embedYouTube:yout frame:CGRectMake(30, 155, 260, 200)];

return YES;

 }

我正在尝试在旋转时更改embedYouTube的大小和位置,但它无效。

它可能正在旋转,但不会改变位置和大小。

2 个答案:

答案 0 :(得分:3)

尝试将代码移动到willAnimateRotationToInterfaceOrientation:方法

答案 1 :(得分:2)

在viewDidLoad方法中,添加一个观察者来监听UIDeviceOrientationDidChangeNotification通知。

[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(didRotate:)
                                      name:@"UIDeviceOrientationDidChangeNotification" object:nil];

您应该使用UIDevice的beginGeneratingDeviceOrientationNotifications开始接收通知。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
在didRotate方法中,获取当前方向,并相应地设置框架。

didRotate方法如下所示。

- (void) didRotate:(NSNotification *)notification { 
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == (UIInterfaceOrientationPortrait))
    [self embedYouTube:yout frame:CGRectMake(30, 155, 260, 200)];

if (orientation == (UIInterfaceOrientationLandscapeRight))
    [self embedYouTube:yout frame:CGRectMake(30, 155, 400, 200)];

if (orientation == (UIInterfaceOrientationLandscapeLeft))
    [self embedYouTube:yout frame:CGRectMake(30, 155, 400, 200)];

if (orientation == (UIInterfaceOrientationPortraitUpsideDown))
    [self embedYouTube:yout frame:CGRectMake(30, 155, 260, 200)]; 
}

并且不要忘记在dealloc方法中删除观察者。

[[NSNotificationCenter defaultCenter] removeObserver:self];