我正在尝试UISlider
控制UIImageView
中显示的图片。 Slider min为0,max为50。
问题是UIImageView
仅对else
和chosenTime = 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];
}
}
答案 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被视为零。