2011-03-11 34 views
1

我已經創建了圖像視圖並在該視圖中設置了圖像,並添加了圖像視圖作爲滾動視圖的子視圖(如幻燈片放映圖像)。所以我想設置圖像視圖框架大小和動態滾動視圖框架大小。我想在縱向和橫向模式下設置不同的圖像視圖幀大小和滾動視圖大小。那麼我怎樣才能在橫向和縱向模式下設置不同的幀尺寸。如何在iPhone中的方向設置不同的圖像視圖框架大小?

這裏我的示例代碼,

- (void)viewDidLoad { 

    [super viewDidLoad]; 

     self.scroll = [[UIScrollView alloc] init]; 

     self.scroll.delegate = self; 

     self.scroll.pagingEnabled = YES; 

     self.scroll.contentMode = UIViewContentModeScaleAspectFit; 

     self.scroll.autoresizingMask = (UIViewAutoresizingFlexibleHeight | 
                     UIViewAutoresizingFlexibleWidth); 

     [self.view addSubview:self.scroll]; 

     totalArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"dashboard_demo_2.png"], 
            [UIImage imageNamed:@"dashboard_demo_1.png"], 
            [UIImage imageNamed:@"dashboard_demo_2.png"], 
            [UIImage imageNamed:@"3.jpg"], 
            [UIImage imageNamed:@"4.jpg"], 
            [UIImage imageNamed:@"12.jpg"], 
            [UIImage imageNamed:@"5.jpg"],nil]; 
     x = 0; 

     for(int i = 0; i< [totalArray count]; i++) 
     { 
       img = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, 320, 200)]; 

       img.image =[totalArray objectAtIndex:i]; 

       x = x+320; // in portait mode if the image sets correctly, but landscape mode it will x = x + 480; 

       [self.scroll addSubview:img]; 

     }  
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

     if (orientation == UIDeviceOrientationPortrait || 
       orientation == UIDeviceOrientationPortraitUpsideDown) 
     { 
       [self portraitMode]; 
     } 
     else if (orientation == UIDeviceOrientationLandscapeLeft || 
         orientation == UIDeviceOrientationLandscapeRight) 
     { 
       [self LandscapeMode]; 

     }    

     return YES; 
} 

-(void) portraitMode 
{ 

     //How to set the frame sizes. 

     self.scroll.frame = CGRectMake(0, 0, 320, 200); 

     img.frame = CGRectMake(x, 0, 320, 200); 

     scroll.contentSize = CGSizeMake(x, 200); // it doesn't set 

} 

-(void) LandscapeMode 
{ 

     //How to set the frame sizes 

     self.scroll.frame = CGRectMake(0, 0, 480, 320); 

     img.frame = CGRectMake(x, 0, 480, 200); 

     scroll.contentSize = CGSizeMake(x, 320); // it doesn't set 

} 

請幫助我。

謝謝。

回答

0

嗨,

你可以註冊到通知中心在屏幕方向變化。

代碼註冊通知中心。

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

落實ScreenRotate方法。

-(void) screenRotate:(NSNotificationCenter*) notification 
{ 

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];  

    if(orientation == UIDeviceOrientationLandscapeLeft) 
    { 

    } 
    else if (orientation == UIDeviceOrientationLandscapeRight) 
    { 

    } 
    else if (orientation == UIDeviceOrientationPortrait) 
    { 

    }  
} 

代碼與通知中心註銷。

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:UIDeviceOrientationDidChangeNotification 
          object:nil]; 



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
     return YES; 
} 

相關問題