0
- (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 的大小和位置,但它不起作用。

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

4

2 回答 2

3

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

于 2012-07-17T13:10:49.233 回答
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];
于 2012-07-17T13:22:25.783 回答