- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if ((fromInterfaceOrientation == UIInterfaceOrientationPortrait) || (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) {
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgImage.png"]];
}
else
{
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgImage1.png"]];
}
}
我使用上面的代码在设备面向横向或纵向时设置self.view.backgroundColor
图像。图像正确更改。
问题:
当我尝试旋转设备时,图像不会快速变化,我的意思是更改图像需要 2 到 3 秒。
如何根据横向或纵向首次设置
self.view.backgroundColor
图像。
请尝试以下操作:
答案1:以VC willRotateToInterfaceOrientation:duration:
方法进行操作。此方法是在界面方向更改时调用的动画方法,它将在背景图像之间提供平滑过渡。
答案 2:使用所需的映像设置self.view.backgroundColor
,viewWillAppear:animated:
方法中考虑self.interfaceOrientation
,这反映了 VC 的当前接口方向。
理想情况下,您可以创建如下方法:
-(void)setBackgroundForInterfaceOrientation:(UIInterfaceOrientation)orientation {
if (UIInterfaceOrientationIsLandscape(orientation)) {
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg-iphone3-landscape"]];
}
else {
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg-iphone3"]];
}
}
现在从任一位置调用此方法:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self setBackgroundForInterfaceOrientation:toInterfaceOrientation];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self setBackgroundForInterfaceOrientation:self.interfaceOrientation];
}
希望对您有所帮助。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIDeviceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
if (currentOrientation == UIInterfaceOrientationLandscapeLeft
|| currentOrientation == UIInterfaceOrientationLandscapeRight)
{
[imageview setImage:[UIImage imageNamed:@"backgroundImage.png"]];
}
else
{
[imageview setImage:[UIImage imageNamed:@"Portrait-backgroundImage.png"]];
}
}
使用它
-(void)viewWillAppear:(BOOL)animated
{
[self willAnimateRotationToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:1.0];
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgImage1.png"]];
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait || (toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown))
{
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgImage.png"]];
}
}