2012-07-17 53 views
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旋轉時的大小和位置,但它不工作。shouldAutorotateToInterfaceOrientation不改變對象位置

它可能正在旋轉,但不會改變位置和大小。

回答

3

嘗試移動你的代碼到willAnimateRotationToInterfaceOrientation:方法

+0

我試過,但沒有也 – 2012-07-17 13:11:20

+0

工作。如果它不是willAnimateRotationToInterfaceOrientation方法中工作,也許這是你的embedYouTube:框架:方法的錯 – iSofTom 2012-07-17 13:14:13

+0

embedYouTube:框架是工作,但是當它旋轉時不改變大小爲適合風景 – 2012-07-17 13:16:18

2

在您的viewDidLoad方法,添加一個觀察者聽UIDeviceOrientationDidChangeNotification通知。

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

您應該使用UIDevice的beginGeneratingDeviceOrientationNotifications開始獲取通知。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

at 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]; 
+0

我必須創建didRotate方法還是內置? – 2012-07-17 13:25:13

+0

你必須創建它。 – 2012-07-17 13:26:29

+2

除非通過調用UIDevice中的方法來開始生成UIDeviceOrientationDidChangeNotification,否則不會被調用,但這不是方法! – iSofTom 2012-07-17 13:27:25