我设置了三个按钮,分别是开始、停止、暂停。并给 NSTimer 控制来计算。开始停止按钮工作正常给了我开始和停止时间但是 pause 按钮没有给我准确的时间。它实际上是暂停时间..但是再次开始它添加了暂停定时和显示[ay。
supoose 我在开始的 5 秒暂停并等待 5 秒然后按开始......它应该显示 5 ......但显示 10 ..
-(void)start:(NSTimer *)timer
{
if(_timer==nil)
{
startDate =[NSDate date];
_timer=[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
}
if(_timer!=nil)
{
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[_timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
}
}
-(void)timer:(NSTimer *)timer
{
NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:startDate];
NSInteger seconds = secondsSinceStart % 60;
NSInteger minutes = (secondsSinceStart / 60) % 60;
NSInteger hours = secondsSinceStart / (60 * 60);
NSString *result = nil;
if (hours > 0)
{
result = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
}
else
{
result = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
}
label.text=result;
NSLog(@"time interval -> %@",result);
}
-(void)stop
{
if(_timer!=nil)
{
endDate = [NSDate date];
NSLog(@"endate%@",endDate);
NSTimeInterval interval = [endDate timeIntervalSinceDate:startDate];
NSLog(@"total time %f",interval);
[_timer invalidate];
_timer = nil;
startDate=nil;
}
}
-(void)pause:(NSTimer *)timer
{
pauseStart = [NSDate dateWithTimeIntervalSinceNow:0];
previousFireDate = [_timer fireDate];
[_timer setFireDate:[NSDate distantFuture]];
}
最佳答案
我创建了这个 applicaion在 mac 操作系统上。我认为您可以理解其中的逻辑,甚至可以通过微小的更改来复制它...至于 UILabel。
在.h中
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
- (IBAction)start:(id)sender;
- (IBAction)pause:(id)sender;
- (IBAction)stop:(id)sender;
@property (strong) IBOutlet NSTextField *label;
@property (strong)NSDate *startDate;
@property (strong)NSTimer *timer;
@property (assign)BOOL isRunning;
@property (assign)BOOL isPaused;
@property(assign)NSInteger secondsSinceStart;
@property(assign)NSInteger secondsPaused;
@end
以.m为单位
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.label.stringValue=@"00:00:00";
self.isRunning=NO;
self.isPaused=NO;
self.secondsPaused=0;
}
-(void)timerDisplay{
if (self.isPaused) {
self.secondsPaused++;
return;
}
self.secondsSinceStart+=1;
NSInteger seconds = self.secondsSinceStart % 60;
NSInteger minutes = (self.secondsSinceStart / 60) % 60;
NSInteger hours = self.secondsSinceStart / (60 * 60);
NSString *result = nil;
if (self.isRunning && !self.isPaused) {
result = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", hours, minutes, seconds];
self.label.stringValue=result;
}
}
- (IBAction)start:(id)sender {
self.isRunning=!self.isRunning;
self.isPaused=NO;
self.secondsSinceStart=0;
self.label.stringValue=@"00:00:00";
self.startDate =[NSDate date];
if (!self.timer) {
self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerDisplay) userInfo:nil repeats:YES];
}
}
- (IBAction)pause:(id)sender {
self.isPaused=!self.isPaused;
NSLog(@"pause : %d",self.isPaused);
}
- (IBAction)stop:(id)sender {
self.isRunning=NO;
NSLog(@"start : %@",self.startDate);
NSLog(@"end : %@",[NSDate date]);
NSLog(@"paused : %ld",self.secondsPaused);
NSInteger totalTime=self.secondsSinceStart+self.secondsPaused;
NSInteger seconds = totalTime % 60;
NSInteger minutes = (totalTime / 60) % 60;
NSInteger hours = totalTime / (60 * 60);
NSString *result = result = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", hours, minutes, seconds];
NSLog(@"Total : %@",result);
}
@end
关于iphone - NSTimer 暂停不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14101880/
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o
我仍然收到标题中的“错误”消息,但不知道如何解决。在ApplicationController中,classApplicationController在routes.rb#match'set_activity_account/:id/:value'=>'users#account_activity',:as=>:set_activity_account--thisdoesn'tworkaswell..resources:usersdomemberdoget:action_a,:action_bendcollectiondoget'account_activity'endend和User
这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw
你能解释一下吗?我想评估来自两个不同来源的值和计算。一个消息来源为我提供了以下信息(以编程方式):'a=2'第二个来源给了我这个表达式来评估:'a+3'这个有效:a=2eval'a+3'这也有效:eval'a=2;a+3'但我真正需要的是这个,但它不起作用:eval'a=2'eval'a+3'我想了解其中的区别,以及如何使最后一个选项起作用。感谢您的帮助。 最佳答案 您可以创建一个Binding,并将相同的绑定(bind)与每个eval相关联调用:1.9.3p194:008>b=binding=>#1.9.3p194:009>eva
我无法运行Spring。这是错误日志。myid-no-MacBook-Pro:myid$spring/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/lib/spring/sid.rb:17:in`fiddle_func':uninitializedconstantSpring::SID::DL(NameError)from/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/li
我在RoR应用程序中有一个提交表单,是使用simple_form构建的。当字段为空白时,应用程序仍会继续下一步,而不会提示错误或警告。默认情况下,这些字段应该是required:true;但即使手动编写也行不通。该应用有3个步骤:NewPost(新View)->Preview(创建View)->Post。我的Controller和View的摘录会更清楚:defnew@post=Post.newenddefcreate@post=Post.new(params.require(:post).permit(:title,:category_id))ifparams[:previewButt
我一直在Heroku上尝试不同的缓存策略,并添加了他们的memcached附加组件,目的是为我的应用程序添加Action缓存。但是,当我在我当前的应用程序上查看Rails.cache.stats时(安装了memcached并使用dalligem),在执行应该缓存的操作后,我得到current和total_items为0。在Controller的顶部,我想缓存我有的Action:caches_action:show此外,我修改了我的环境配置(对于在Heroku上运行的配置)config.cache_store=:dalli_store我是否可以查看其他一些统计数据,看看它是否有效或我做错
我在我的机器上安装了ruby版本1.9.3,并且正在为我的个人网站开发一个octopress项目。我为我的gems使用了rvm,并遵循了octopress.org记录的所有步骤。但是我在我的rake服务器中发现了一些错误。这是我的命令日志。Tin-Aung-Linn:octopresstal$ruby--versionruby1.9.3p448(2013-06-27revision41675)[x86_64-darwin12.4.0]Tin-Aung-Linn:octopresstal$rakegenerate##GeneratingSitewithJekyllidenticals
我是RubyonRails的新手,我正在尝试编写一个morethan表达式:5%>大于号不断抛出异常捕获错误。我不确定如何解决这个问题?编辑:这不是rails,也不是View,它是一个Ruby构造 最佳答案 使用5%>错误来自photo_limit而不是从Integer延伸类(猜测它真的是一个字符串),因此没有混合比较方法/s有关更多信息,请参阅:http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/特别是你必须混入Comparable并定义方法。虽然在这