UISlider控制UIImageView

时间:2012-02-16 11:28:18

标签: iphone ios uiimageview uislider

我正在尝试UISlider控制UIImageView中显示的图片。 Slider min为0,max为50。

问题是UIImageView仅对elsechosenTime = 50作出反应。 只有这时相应的图片才显示在UIImageView中。 忽略48和49,在这些情况下显示“其他”图片。 非常感谢任何帮助!

以下是代码:

-(IBAction) sliderChanged:(id)sender{
    UISlider *slider = (UISlider *) sender;
    int prog = (int)(slider.value + 0.5f);
    NSString * newText = [[NSString alloc] initWithFormat:@"%d", prog];
    sliderLabel.text = newText;
    int chosenTime = [newText intValue];

    NSLog(@"chosenTime is %i", chosenTime);
    //chosenTime is confirmed int value in the NSLOG!!!

    if (chosenTime == 1) {
        NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Pic0001.png"];
        clockView.image = [UIImage imageWithContentsOfFile:fullpath];
    }
    if (chosenTime == 48) {
        NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Pic0048.png"];
        clockView.image = [UIImage imageWithContentsOfFile:fullpath];
    } 
    if (chosenTime == 49) {
        NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Pic0049.png"];
        clockView.image = [UIImage imageWithContentsOfFile:fullpath];
    } 
    if (chosenTime == 50) {
        NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Pic0050.png"];
        clockView.image = [UIImage imageWithContentsOfFile:fullpath];
    } else {
        NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Pic0000.png"];
        clockView.image = [UIImage imageWithContentsOfFile:fullpath];
    }
}

1 个答案:

答案 0 :(得分:0)

我简化了一点代码。确保NSLogged编号在1-50范围内。如果不是,则滑块的最小值/最大值配置错误。

-(IBAction) sliderChanged: (UISlider *) sender
{
    int chosenTime = (int) ceilf(slider.value);
    NSLog(@"chosenTime is %i", chosenTime);
    //chosenTime is confirmed int value in the NSLOG!!!

    if (chosenTime > 50)
        chosenTime = 0;

    NSString *fileName = [NSString stringWithFormat: @"Pic00%02d.png", chosenTime];
    NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: fileName];
    clockView.image = [UIImage imageWithContentsOfFile: fullpath];
}

您的代码存在的问题是每个else后缺少if。比较逐渐变为最后if,之后是50或不是50,而不是50被视为零。