gpt4 book ai didi

ios - 显示启用/禁用时间单位的倒计时

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:37:15 28 4
gpt4 key购买 nike

这个问题基本上是关于自定义日期格式的。我设法开发了一个倒计时应用程序,该应用程序设法创建一个事件,该事件稍后可以由客户查看、定制或跟踪。重要的部分是倒数计时器,它从数据库中获取秒数并根据日期格式化程序显示剩余时间。到目前为止,我一直在应用程序中使用默认设置,计数器格式为 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 秒,直到 The counter

例如,这些日历单位不能放在一个数组中,那么如果用户选择 2、4、5、6 将是月:天:小时:分钟,我将如何选择日历格式?我不能说 components = [NSArray arrayWithObjects: [arrComponents objectAtIndex:2], [arrComponents objectAtIndex:2],nil];

User options

有什么想法吗?

最佳答案

好吧,这比我最初想象的要难


在整个系统中,单位标志用于将日期定义为组件等。这个单位缺陷可以组合成一个值,一个位掩码。我将它用于我的代码。

年份单位可能是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 ^= sbitmask = 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,我们假设它已设置并将它附加到字符串。


结果:

enter image description here


完整的代码:


#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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com