gpt4 book ai didi

ios - 从按下的按钮获取时间(按住)

转载 作者:行者123 更新时间:2023-11-28 22:29:15 26 4
gpt4 key购买 nike

在尝试获取时间时,我没有得到预期的值。

当我运行这个时,时间是 19:59。而在 timeDifferenceString 中,Value 为 71976.894206。我认为这是当前时间。
我计算
71976/3600 = 19,99 h
0,99 * 60 = 59,4 米
0,4 * 60 = 24 秒

所以我得到时间 19:59:24 点钟。但我想得到第一次和第二次之间的区别,而不是当前时间(以秒为单位)。

我只想得到我(按住)按下按钮的时间
以及未按下按钮的时间
(如果我按下按钮则第一次开始,如果我按下另一个按钮则停止)。

- (IBAction)pressTheButton:(id)sender {
NSDate * now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm:ss:SSS"];
NSString *currentTime = [formatter stringFromDate:now];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components; //= [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:10];

NSDate* firstDate = [NSDate date];
NSString *getTime = [formatter stringFromDate:now];

getTime = [formatter stringFromDate:firstDate];

if (iX==0)
{
components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
firstDate = [calendar dateFromComponents:components];
getTime = [formatter stringFromDate:firstDate];
//firstDate = [NSDate date];//[dateFormatter dateFromString:@"00:01:00:090"];
}

components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
//NSDate* secondDate = [dateFormatter dateFromString:@"10:01:02:007"];
NSDate * secondDate = [calendar dateFromComponents:components];
getTime = [formatter stringFromDate:firstDate];

NSTimeInterval timeDifference = [firstDate timeIntervalSinceDate:secondDate];
NSString *timeDifferenceString = [NSString stringWithFormat:@"%f", timeDifference];

timeDiff[iX] = [timeDifferenceString intValue];
[timeLabel setText:timeDifferenceString];
iX++;
}

如果您对我的问题有更好的解决方案,请帮助我。

最佳答案

看UIControlEventTouchDown,UIControlEventTouchUp里面:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html

然后我想你想存储一个 NSDate,当你按下某种 ivar 或属性 (UIControlEventTouchDown) 和另一个当你释放按钮(UIControlEventTouchUpInside 或 UIControlEventTouchUpOutside),然后在那个日期调用 -timeIntervalSinceDate:。所以它看起来大致像这样:

- (IBAction)pressedDown:(id)sender {
self.pressedDownDate = [NSDate date];
}

- (IBAction)touchedUp:(id)sender {
NSDate *now = [NSDate date];
NSLog(@"Time passed: %d", [now timeIntervalSinceDate:self.pressedDownDate]);
}

关于ios - 从按下的按钮获取时间(按住),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17954443/

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