2011-07-02 3 views
0

Je souhaite modifier ma disposition de manière dynamique. comment je définis dans mon code? Dans mon code, je définirais commechanger l'orientation de manière dynamique

const CGFloat kScrollObjHeight = 460.0; 
const CGFloat kScrollObjWidth = 320.0; 
const NSUInteger kNumImages  = 32; 

- (void)layoutScrollImages 
{ 
    UIImageView *view = nil; 
    NSArray *subviews = [scrollView1 subviews]; 

    // reposition all image subviews in a horizontal serial fashion 
    CGFloat curXLoc = 0; 
    for (view in subviews) 
    { 
     if ([view isKindOfClass:[UIImageView class]] && view.tag > 0) 
     { 
      CGRect frame = view.frame; 
      frame.origin = CGPointMake(curXLoc, 0); 
      view.frame = frame; 

      curXLoc += (kScrollObjWidth); 
     } 
    } 

    // set the content size so it can be scrollable 
    [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)]; 
} 

- (void)viewDidLoad 
{ 
    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; 

    // 1. setup the scrollview for multiple images and add it to the view controller 
    // 
    // note: the following can be done in Interface Builder, but we show this in code for clarity 
    [scrollView1 setBackgroundColor:[UIColor blackColor]]; 
    [scrollView1 setCanCancelContentTouches:NO]; 
    scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
    scrollView1.clipsToBounds = YES;  // default is NO, we want to restrict drawing within our scrollview 
    scrollView1.scrollEnabled = YES; 

    //imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image0.jpg"]]; 
    [scrollView1 addSubview:imageView]; 
    [scrollView1 setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)]; 
    scrollView1.minimumZoomScale = 1; 
    scrollView1.maximumZoomScale = 3; 
    scrollView1.delegate = self; 
    [scrollView1 setScrollEnabled:YES]; 

    // pagingEnabled property default is NO, if set the scroller will stop or snap at each photo 
    // if you want free-flowing scroll, don't set this property. 
    scrollView1.pagingEnabled = YES; 

    // load all the images from our bundle and add them to the scroll view 
    NSUInteger i; 
    for (i = 1; i <= kNumImages; i++) 
    { 
     NSString *imageName = [NSString stringWithFormat:@"page-%d.jpg", i]; 
     UIImage *image = [UIImage imageNamed:imageName]; 
     UIImageView *ImageView = [[UIImageView alloc] initWithImage:image]; 

     // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
     CGRect rect = ImageView.frame; 
     rect.size.height = kScrollObjHeight; 
     rect.size.width = kScrollObjWidth; 
     ImageView.frame = rect; 
     ImageView.tag = i; // tag our images for later use when we place them in serial fashion 
     [scrollView1 addSubview:ImageView]; 
     [ImageView release]; 
    } 

    [self layoutScrollImages]; // now place the photos in serial layout within the scrollview 


} 

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView { 
    return imageView; 
} 


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

    return YES; 
} 


- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 


    switch(toInterfaceOrientation){ 
     case (UIInterfaceOrientationLandscapeLeft): 
     { 
      landscapeModeViewController=[[LandscapeModeViewController alloc] initWithNibName:@"LandscapeModeViewController" bundle:nil]; 
      [self.view addSubview:landscapeModeViewController.view]; 
      /*UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message:@"change Mode" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [info show]; 
      [info release];*/ 
     } 
      break; 
     case(UIInterfaceOrientationLandscapeRight): 
     { 
      //landscapeModeViewController=[[LandscapeModeViewController alloc] initWithNibName:@"LandscapeModeViewController" bundle:nil]; 
      //[self.view addSubview:landscapeModeViewController.view]; 
      UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message:@"change Mode" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [info show]; 
      [info release]; 
     } 
      break; 
     case(UIInterfaceOrientationPortrait): 
     { 
      //portraitMode=[[PortraitMode alloc] initWithNibName:@"PortraitMode" bundle:nil]; 
      //[self.view addSubview:portraitMode.view]; 
      UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message:@"change Mode" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [info show]; 
      [info release]; 

     } 
      break; 

      /*if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
      UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message:@"Select Landscape mode." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [info show]; 
      [info release]; 
      [scrollView1 release]; 
      landscapeModeViewController=[[LandscapeModeViewController alloc] initWithNibName:@"LandscapeModeViewController" bundle:nil]; 
      [self.view addSubview:landscapeModeViewController.view]; 


      } 


      else{ 
      UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message:@"Select Portrait Mode" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [info show]; 
      [info release]; 

      portraitMode=[[PortraitMode alloc] initWithNibName:@"PortraitMode" bundle:nil]; 
      [self.scrollView1 addSubview:portraitMode.view]; 
      } */ 


    } 
} 
- (void)dealloc 
{  
    [scrollView1 release]; 
    [imageView release]; 

    [super dealloc]; 
} 

- (void)didReceiveMemoryWarning 
{ 

    [super didReceiveMemoryWarning]; 
} 

@end 

dans le code ci-dessus lorsque l'appareil rotate puis exécuter uialerview mais vue précédente et ne pas libérer le mode de landsacpe montré derrière lui. alors qu'est-ce que je vais faire pour ça?

Répondre

2

Vous pouvez définir un masque de mise à jour automatique pour chaque vue de votre mise en page comme vous le souhaitez.

Par exemple, si vous voulez bouton pour afficher toujours dans le coin en haut à gauche, vous pouvez définir un masque de redimensionnement automatique en haut de page flexible et gauche ..

Si votre mise en page est tout à fait différent en mode portrait et paysage, alors vous avez besoin pour définir des cadres dans willRotateToInterfaceOrientation. Par exemple, si vous voulez que votre bouton soit en haut à gauche en mode portrait et au centre en mode paysage, vous devez définir le cadre dans la méthode de délégation ci-dessus.

Modifier Réponse:

if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){ 
    if(!landscapeModeViewController){ 
     landscapeModeViewController=[[LandscapeModeViewController alloc] initWithNibName:@"LandscapeModeViewController" bundle:nil]; 
    } 
    [self.view addSubview:landscapeModeViewController.view]; 
    [self.view bringSubviewToFront:landscapeModeViewController.view]; 
} 
else if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){ 
    if(!portraitMode){ 
     portraitMode=[[PortraitMode alloc] initWithNibName:@"PortraitMode" bundle:nil]; 
    } 
    [self.view addSubview:portraitMode.view]; 
    [self.view bringSubviewToFront:portraitMode.view]; 
} 
+0

merci ur réponse mais je l'essayer pas à moi. J'ai éditer ma question alors s'il vous plaît vérifier donner une autre solution. – ram

+0

Vérifiez ma réponse mise à jour, si cela fonctionne ... – iMOBDEV

+0

merci pour votre suggestion. – ram

Questions connexes