2012-02-16 81 views
0

我想讓UISlider控制UIImageView中顯示的圖片。 Slider min是0,max是50.UISlider控制UIImageView

問題是UIImageView只對elsechosenTime = 50作出反應。 只有在UIImageView中顯示相應的圖片。 48和49被忽略,這些情況下顯示「else」圖片。 任何幫助非常感謝!

下面的代碼:

-(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]; 
    } 
} 

回答

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]; 
} 

與您的代碼的問題是每個if後沒有else。比較結果會落到最後的if,之後是50或不是50,而不是50被視爲零。

+0

非常感謝,偉大的學習時刻! – Giel 2012-02-17 09:02:38