【发布时间】:2011-12-26 14:54:36
【问题描述】:
我需要一个滑块,用于在滑块标签中显示从上午 12 点到晚上 11 点到 45 点的实时时间,步长为 15 分钟。但我只是不知道该怎么做。有人可以给我一些建议吗?
【问题讨论】:
标签: iphone objective-c ios ipad uislider
我需要一个滑块,用于在滑块标签中显示从上午 12 点到晚上 11 点到 45 点的实时时间,步长为 15 分钟。但我只是不知道该怎么做。有人可以给我一些建议吗?
【问题讨论】:
标签: iphone objective-c ios ipad uislider
如果您只需要一个带滑块的时间选择器,请在sliderChanged中使用以下代码:处理程序:
- (IBAction)onSliderChange:(id)sender {
UISlider *slider = (UISlider *)sender;
NSUInteger numberOfSlots = 24*4 - 1; //total number of 15mins slots
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"h-mm a"];
NSDate *zeroDate = [dateFormatter dateFromString:@"12-00 AM"];
NSUInteger actualSlot = roundf(numberOfSlots*slider.value);
NSTimeInterval slotInterval = actualSlot * 15 * 60;
NSDate *slotDate = [NSDate dateWithTimeInterval:slotInterval sinceDate:zeroDate];
[dateFormatter setDateFormat:@"h-mm a"];
self.timeLabel.text = [dateFormatter stringFromDate:slotDate];
}
【讨论】:
创建一个滑块,在视图控制器中,让滑块随着滑块上的值的变化而改变标签。要设置值的范围,请更改情节提要中属性下的范围(您也可以将更改间隔设置为 15 步)。您可能想给它以分钟或类似的时间间隔,然后将其转换为时间:)
- (IBAction)sliderChanged:(UISlider *)sender
【讨论】:
我正在添加另一个答案来添加代码。是的,您可以从滑块中获取值,然后在将值转换为时间后设置标签的文本。像这样:
// Add 0.5 to slider value and truncate it to an integer by type casting it as integer
int sliderIntValue = (int)([sender value] + 0.5f);
//do some conversion and set the label to the value
self.sliderLabel.text = newLabelText;
【讨论】:
您可以使用NSTimer 每 15 分钟调用一次方法,并将滑块/标签更新为当前时间。
您可能想查看NSDateFormatter,获取适合您日期的字符串表示形式可能很有用。
【讨论】: