3

縦向きモードと横向きモードの 2 つの画像があります。モバイル デバイスのビューの回転が発生したときに、これらの画像を切り替える最良の方法は何ですか?

現在、縦長の画像を表示するだけです。また、デバイスが横向きモードに回転すると、縦向きの画像は単純に引き伸ばされます。

向きの回転ハンドラー内でチェックし、画像を適切な向きの画像にリセットするだけです (つまり、向きに基づいて手動で設定します)??

ありがとう!

4

1 に答える 1

2

3つの方法を見つけました。最後の方法が良いと思います

1:自動サイズ変更

例:

UIImageView *myImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImage.png"]];    
myImageView.frame = self.view.bounds;
myImageView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight
myImageView.contentMode = UIViewContentModeScaleAspectFill;    
[self.view addSubview:myImageView]; 
[imageView release];

2: CGAffineTransformMakeRotation

例:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                        duration:(NSTimeInterval)duration {
  if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {        
                myImageView.transform = CGAffineTransformMakeRotation(M_PI / 2);
  }
  else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
                myImageView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
  }
  else {
             myImageView.transform = CGAffineTransformMakeRotation(0.0);
  }
}

3: Interface Builder で myImageView の自動サイズ設定を画面の自動塗りつぶしに設定する

例:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){
    myImageView.image = [UIImage imageNamed:@"myImage-landscape.png"];
} else  if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){
    myImageView.image = [UIImage imageNamed:@"myImage-portrait.png"];
} }

その他のソリューションはこちら

developer.apple ソリューションはこちら

于 2012-07-31T05:43:02.243 に答える