- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这个问题基本上是关于自定义日期格式的。我设法开发了一个倒计时应用程序,该应用程序设法创建一个事件,该事件稍后可以由客户查看、定制或跟踪。重要的部分是倒数计时器,它从数据库中获取秒数并根据日期格式化程序显示剩余时间。到目前为止,我一直在应用程序中使用默认设置,计数器格式为 dd:hh:mm,显然我的客户希望允许应用程序用户自定义时间的显示方式。所以现在用户可以选择四个选项,例如 yy:weeks:hh:mm 或 months:weeks:days:hours。我认为最好的方法是将表的四个索引作为字符串保存到 db。我的问题出在日历上。
conversionInfo = [sysCalendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSSecondCalendarUnit fromDate:today toDate:future options:0];
one = [NSString stringWithFormat:@"%d Days %d Hours %d Minutes %d Seconds %@", [conversionInfo day], [conversionInfo hour],[conversionInfo minute],[conversionInfo second], untilOrSince]; self.eventTimer.text = one;
conversionInfo = [sysCalendar components: NSHourCalendarUnit | NSSecondCalendarUnit fromDate:today toDate:future options:0];
one = [NSString stringWithFormat:@"%ld Seconds %@", (long)[conversionInfo second], untilOrSince];
此外,如果我用所有单位构建日历但只显示秒,那么倒计时将只显示 59 秒而不是 343746545 秒,直到
例如,这些日历单位不能放在一个数组中,那么如果用户选择 2、4、5、6 将是月:天:小时:分钟,我将如何选择日历格式?我不能说 components = [NSArray arrayWithObjects: [arrComponents objectAtIndex:2], [arrComponents objectAtIndex:2],nil];
有什么想法吗?
最佳答案
好吧,这比我最初想象的要难
在整个系统中,单位标志用于将日期定义为组件等。这个单位缺陷可以组合成一个值,一个位掩码。我将它用于我的代码。
年份单位可能是0....000010
月份单位可能是 0....000100
两者的位掩码都是
0....000010 NSCalendarUnitYear
0....000100 NSCalendarUnitMonth
------------
& 0....000110
因此,要保存要使用一个单位的信息,我们只需将适当的位设置为 1
- (void)viewDidLoad
{
[super viewDidLoad];
self.selectedUnitsBitmask= 0;
self.unitNames = @[ @"Year", @"Month", @"Week", @"Day", @"Hour", @"Minute", @"Second"];
self.units = @[@(NSCalendarUnitYear), @(NSCalendarUnitMonth), @(NSCalendarUnitWeekOfMonth), @(NSCalendarUnitDay), @(NSCalendarUnitHour), @(NSCalendarUnitMinute), @(NSCalendarUnitSecond)];
NSDateComponents *c = [[NSDateComponents alloc] init];
c.year = 2014;
c.month = 12;
c.day = 25;
self.futureDate = [[NSCalendar currentCalendar] dateFromComponents:c];
}
self.unitNames
的值用于填充 TableView 。self.units
存储每个可用单元的单元标志。
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = _unitNames[indexPath.row];
NSUInteger s;
NSUInteger row = indexPath.row;
s = NSIntegerMax & [self.units[row] integerValue];
cell.accessoryType = (self.selectedUnitsBitmask & s) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
如果选择了一个单元格,s = NSIntegerMax & [self.units[row] integerValue];
将检查位掩码,如果要使用这个单元的话。
如果选择了一个单元格,我们想要切换它所代表的单位的位
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger s;
NSUInteger row = indexPath.row;
s = NSIntegerMax & [self.units[row] integerValue];
self.selectedUnitsBitmask ^= s ;
NSLog(@"%lu, %lu", (unsigned long)s, (unsigned long)self.selectedUnitsBitmask);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType =(self.selectedUnitsBitmask & s) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
[self updateCountDownLabel:nil];
}
这里
NSUInteger s;
NSUInteger row = indexPath.row;
s = NSIntegerMax & [self.units[row] integerValue];
s
代表选中的位,
self.selectedUnitsBitmask ^= s ;
将在位掩码中切换它。 bitmask ^= s
是 bitmask = bitmask ^ s
的简写形式,其中 ^
是异或:位掩码的第 n 位有与 s 的第 n 位相反
0011 bitmask
^ 0101 s
----
= 1100
现在我们可以根据在位掩码中找到的单位标志更新将打印时差的字符串:
- (IBAction)updateCountDownLabel:(id)sender {
BOOL includeYear = self.selectedUnitsBitmask & NSCalendarUnitYear;
BOOL includeMonth = self.selectedUnitsBitmask & NSCalendarUnitMonth;
BOOL includeDay = self.selectedUnitsBitmask & NSCalendarUnitDay;
BOOL includeHour = self.selectedUnitsBitmask & NSCalendarUnitHour;
BOOL includeMinute= self.selectedUnitsBitmask & NSCalendarUnitMinute;
BOOL includeSecond= self.selectedUnitsBitmask & NSCalendarUnitSecond;
NSDateComponents *diffDateComponents = [[NSCalendar currentCalendar] components:self.selectedUnitsBitmask fromDate:[NSDate date] toDate:self.futureDate options:0];
NSMutableString *outputString = [@"" mutableCopy];
if (includeYear && diffDateComponents.year)
[outputString appendFormat:@"%d Year", diffDateComponents.year];
if (includeMonth && diffDateComponents.month)
[outputString appendFormat:@" %d Month", diffDateComponents.month];
if (diffDateComponents.weekOfMonth < NSIntegerMax && diffDateComponents.weekOfMonth)
[outputString appendFormat:@" %d Week", diffDateComponents.weekOfMonth];
if (includeDay && diffDateComponents.day)
[outputString appendFormat:@" %d Day", diffDateComponents.day];
if (includeHour && diffDateComponents.hour)
[outputString appendFormat:@" %d Hour", diffDateComponents.hour];
if (includeMinute && diffDateComponents.minute)
[outputString appendFormat:@" %d Minute", diffDateComponents.minute];
if (includeSecond && diffDateComponents.second)
[outputString appendFormat:@" %d Second", diffDateComponents.second];
self.countDownLabel.text = [outputString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
要确定是否设置了标志,我们使用 &
AND 运算符
BOOL includeYear = self.selectedUnitsBitmask & NSCalendarUnitYear;
如果年份标志是 0....000010
位掩码包含0....010110
&
操作将返回
0....000010 NSCalendarUnitYear
& 0....010110 bitmask
-------------
= 0....000010 -> NSCalendarUnitYear
我们为一周内的所有单位执行此操作。我不知道为什么,但 & 操作总是返回 0。这里我们使用 hack:
if (diffDateComponents.weekOfYear < NSIntegerMax)
[outputString appendFormat:@" %d Week", diffDateComponents.weekOfYear];
对于新的 NSDateComponents week
是用整数 NSIntegerMax 表示的最大数字实例化的。如果它的值小于 NSIntegerMax,我们假设它已设置并将它附加到字符串。
结果:
完整的代码:
#import "ViewController.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *countDownLabel;
@property (nonatomic) NSInteger selectedUnitsBitmask;
@property (strong, nonatomic) NSArray *unitNames;
@property (strong, nonatomic) NSArray *units;
@property (strong, nonatomic) NSDate *futureDate;
- (IBAction)updateCountDownLabel:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.selectedUnitsBitmask= 0;
self.unitNames = @[ @"Year", @"Month", @"Week", @"Day", @"Hour", @"Minute", @"Second"];
self.units = @[@(NSCalendarUnitYear), @(NSCalendarUnitMonth), @(NSCalendarUnitWeekOfMonth), @(NSCalendarUnitDay), @(NSCalendarUnitHour), @(NSCalendarUnitMinute), @(NSCalendarUnitSecond)];
NSDateComponents *c = [[NSDateComponents alloc] init];
c.year = 2014;
c.month = 12;
c.day = 25;
self.futureDate = [[NSCalendar currentCalendar] dateFromComponents:c];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.units count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = _unitNames[indexPath.row];
NSUInteger s;
NSUInteger row = indexPath.row;
s = NSIntegerMax & [self.units[row] integerValue];
cell.accessoryType = (self.selectedUnitsBitmask & s) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger s;
NSUInteger row = indexPath.row;
s = NSIntegerMax & [self.units[row] integerValue];
self.selectedUnitsBitmask ^= s ;
NSLog(@"%lu, %lu", (unsigned long)s, (unsigned long)self.selectedUnitsBitmask);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType =(self.selectedUnitsBitmask & s) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
[self updateCountDownLabel:nil];
}
- (IBAction)updateCountDownLabel:(id)sender {
BOOL includeYear = self.selectedUnitsBitmask & NSCalendarUnitYear;
BOOL includeMonth = self.selectedUnitsBitmask & NSCalendarUnitMonth;
BOOL includeDay = self.selectedUnitsBitmask & NSCalendarUnitDay;
BOOL includeHour = self.selectedUnitsBitmask & NSCalendarUnitHour;
BOOL includeMinute= self.selectedUnitsBitmask & NSCalendarUnitMinute;
BOOL includeSecond= self.selectedUnitsBitmask & NSCalendarUnitSecond;
NSDateComponents *diffDateComponents = [[NSCalendar currentCalendar] components:self.selectedUnitsBitmask fromDate:[NSDate date] toDate:self.futureDate options:0];
NSMutableString *outputString = [@"" mutableCopy];
if (includeYear && diffDateComponents.year)
[outputString appendFormat:@"%d Year", diffDateComponents.year];
if (includeMonth && diffDateComponents.month)
[outputString appendFormat:@" %d Month", diffDateComponents.month];
if (diffDateComponents.weekOfMonth < NSIntegerMax && diffDateComponents.weekOfMonth)
[outputString appendFormat:@" %d Week", diffDateComponents.weekOfMonth];
if (includeDay && diffDateComponents.day)
[outputString appendFormat:@" %d Day", diffDateComponents.day];
if (includeHour && diffDateComponents.hour)
[outputString appendFormat:@" %d Hour", diffDateComponents.hour];
if (includeMinute && diffDateComponents.minute)
[outputString appendFormat:@" %d Minute", diffDateComponents.minute];
if (includeSecond && diffDateComponents.second)
[outputString appendFormat:@" %d Second", diffDateComponents.second];
self.countDownLabel.text = [outputString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
关于ios - 显示启用/禁用时间单位的倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21289670/
我有 2 张 table ; item_in(item_id,unit) item_out(item_id,unit) 现在假设我想知道为每个项目插入了多少个单元,我只是查询 select sum(u
API 浏览器中有 3 个速率限制类别: 如果我使用 Youtube 数据 API(其中跟随 implicit OAuth grant flow)创建客户端 Web 应用程序,我是否仍被限制为总共 1
我正在使用一个 postgresql 表,该表包含许多带有 GEOMETRY(Point, 4326) 的行。使用 ST_SnapToGrid 函数和 DISTINCT 选择,我只根据显示的 map
我对 C++ 和 Cppunit 都很陌生。我正在尝试编译一个小的 cppunit 测试。但是,我没有成功。 qwerty@qwerty:~/chessgame/src$ g++ -Wall Coor
我注意到 REM 单位可用于元素的大小,而不仅仅是字体大小。并且对 HTML 字体大小属性非常有用。 html { font-size:1vw } @media all and (max-width:
我试图在 Shapely 中找到线串的长度(以米为单位),但似乎无法达到预期的结果。几乎可以肯定我在坐标系方面犯了一些错误,但我无法弄清楚。 这是单行的一些简化代码: from shapely.geo
对于大量的物种数据集,我试图计算给定月份集的圆形平均值,例如对于从 3 月到 7 月开花的物种,我想知道开花的平均月份(即 5 月),以及围绕平均值的方差。 给定月份是循环的,因此 12 月到 2 月
我还应该在单元测试中释放对象吗? 我注意到在Apple的“iPhoneUnitTests”示例项目中,设置方法中的对象是[[object alloc] init],但从未在单元测试中的任何地方发布?
我目前正在使用 OpenGL 进行开发,并使用米作为我自己的单位,即 20 厘米宽的三角形为 0.2。然而 OpenGL 似乎对这些数字进行了舍入,最终的形状并不完全符合我的意愿。这在 OpenGL
我的问题与对信号进行频谱分析或将信号放入 FFT 并使用合适的数值包解释结果的物理意义有关, 具体: 获取一个信号,例如时变电压 v(t) 将其放入 FFT 中(您将得到复数序列) 现在取模 (abs
在深入研究代码后,我意识到 Fabricjs Text 对象的 fontSize 是在 PIXELS 中测量的。在我的项目中,有时我需要使用点而不是像素。 当指定单位时,我只在代码中找到一个位置,此片
在我的应用程序中,我尝试使用,RentModel.find({prop_location : { $near : [msg.lat, msg.lng],$maxDistance : 500}},函数(
我正在开发我的第一个 LibGdx (Scene2d + Box2d) 游戏,这对我来说是一个全新的领域,但仍然对一些事情感到有点困惑,尤其是关于单位。已经看到了几种不同的处理方法,但仍然不确定哪种方
我正在寻找一个 MySQL 查询(子查询很好),它将以下列格式获取过去一年中每个订单的单位分布: units_per_order | number_of_orders |
我正在使用 Highcharts生成折线图。 我遇到了 numberFormat 的问题: var test = 15975000; numberFormat(test, 0,',','.'); 结果
我正在尝试创建一些用户定义的类型来表示单位,以便我可以强类型化函数参数。例如,长度为毫米,速度为毫米每秒,加速度为毫米每秒等。 到目前为止我已经这样做了: template struct Value
谁能解释一下最低精度的 ULP 单位?我有如下定义,但还是不清楚 “表示分数时的误差大小与存储的数字大小成正比。ULP 或最小精度单位定义了存储数字时可以获得的最大误差。存储的数字越大,ULP 越大”
我有一张卡片图像,我需要重复它 30 次,每次我想申请一张特定卡片的左侧位置时,它会与卡片重叠,然后再停留在一副牌的位置上。 问题是,当我将左侧位置应用于图像卡片时,它会将相同的左侧位置应用于所有卡片
有没有办法用php代码更改每个滚动条的大小。 说明:当我向下滚动时,它会下降x(50~)像素,我想将x改为20。 编辑:这是我的代码。。。 Excel "; $i=1; wh
我不知道下面的想法是否可行或不能概括它,但我想将每个计算值四舍五入到 100 单位四舍五入。 例子: double x; int x_final; ... if (x<400) x_final=400
我是一名优秀的程序员,十分优秀!