我为所有 iOS 设备创建了以下代码。它检测屏幕尺寸并为每个设备尺寸使用适当的图像。虽然这可行,但它远非理想。对于每张图片,我需要将其保存为 5 种不同的尺寸。有什么更好的方法来做到这一点?我基本上应该有一个背景(正在重复),中心有一个小图标和一些文字。我可以只生成具有正确大小字体的文本并使用单独的图像视图大小而不是 40 多个图像。如果这是我应该这样做的方式...如何使背景图像视图流过屏幕边缘而不拉伸背景图像并使其看起来失真?有更好的选择吗?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *beginIntro = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[beginIntro addTarget:self
action:@selector(begin:)
forControlEvents:UIControlEventTouchDown];
[beginIntro setTitle:@"Begin Intro" forState:UIControlStateNormal];
beginIntro.frame = CGRectMake(80.0, 310.0, 160.0, 40.0);
[[self view] addSubview:beginIntro];
UIButton *reset = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[beginIntro addTarget:self
action:@selector(reset:)
forControlEvents:UIControlEventTouchDown];
[reset setTitle:@"Reset Intro" forState:UIControlStateNormal];
reset.frame = CGRectMake(80.0, 410.0, 160.0, 40.0);
[[self view] addSubview:reset];
_SlideImageView = [[UIImageView alloc]initWithFrame:CGRectMake([[UIScreen mainScreen]bounds].origin.x, [[UIScreen mainScreen]bounds].origin.y, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height)];
[[self view]addSubview:_SlideImageView];
[self setSlideNumber:[NSNumber numberWithInt:1]];
}
-(IBAction)begin:(id)sender{
if ([_SlideNumber intValue] <=8) {
UIImage * toImage = [UIImage imageNamed:[self getSlideShowImageName]];
[UIView transitionWithView:self.view
duration:5.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.SlideImageView.image = toImage;
} completion:^(BOOL finished) {
sleep(2);
[self begin:nil];
}];
int SlideNumber = [_SlideNumber intValue];
int NewSlideNumber = SlideNumber+=1;
[self setSlideNumber:[NSNumber numberWithInt:NewSlideNumber]];
}
}
-(NSString*)getSlideShowImageName{
NSString * slideShowImageName = [NSString stringWithFormat:@""];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale > 1.0)
{
//iPad retina screen
slideShowImageName = [NSString stringWithFormat:@"ipad%d@2x.png",[_SlideNumber intValue]];
}
else
{
//iPad screen
slideShowImageName = [NSString stringWithFormat:@"ipad%d.png",[_SlideNumber intValue]];
}
}
else
{
if ([UIScreen instancesRespondToSelector:@selector(scale)])
{
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale > 1.0)
{
if([[ UIScreen mainScreen ] bounds ].size.height == 568)
{
//iphone 5
slideShowImageName = [NSString stringWithFormat:@"iphone5%d.png",[_SlideNumber intValue]];
}
else
{
//iphone retina screen
slideShowImageName = [NSString stringWithFormat:@"iphoneipod%d@2x.png",[_SlideNumber intValue]];
}
}
else
{
//iphone screen
slideShowImageName = [NSString stringWithFormat:@"iphoneipod%d.png",[_SlideNumber intValue]];
}
}
}
NSLog(@"slideShowImageName : %@",slideShowImageName);
return slideShowImageName;
}
-(IBAction)reset:(id)sender{
[self setSlideNumber:[NSNumber numberWithInt:0]];
[self begin:nil];
}