iOS在多个设备上的最佳幻灯片练习

时间:2013-08-03 22:35:20

标签: ios uiimageview slideshow

我为所有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];
}

1 个答案:

答案 0 :(得分:0)

要使背景图案图像“重复”以填充整个屏幕,请使用背景图案图像创建UIColor,并将视图背景颜色设置为该颜色。例如:

UIColor *myBackgroundPattern = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myBackgroundPatternImageFilename"]];
myView.backgroundColor = myBackgroundPattern;

请注意,如果图像文件为png,则无需添加扩展名。

修改

使用单个图像填充背景而不“拉伸”:

myImageView.image = [UIImage imageNamed:@"MyBackgroundImageFilename"];
myImageView.contentMode = UIViewContentModeCenter;

假设图像至少与最大设备显示一样大,它将居中并剪裁到myImageView的边界。