我正在计算用户将手指放在按钮上的时间, 并以秒为单位显示此时间作为label.text ...(最多10秒) 一切正常,但label.text在动作结束时更新 (我只有一个按钮和一个标签......)
我的标题:
#import <UIKit/UIKit.h>
#import <CoreFoundation/CFData.h>
@interface TwisterViewController : UIViewController {
UILabel *label;
CFTimeInterval presstime;
}
@property(nonatomic,retain) IBOutlet UILabel *label;
-(IBAction) fingeron;
@end
我的档案
#import "TwisterViewController.h"
@implementation TwisterViewController
@synthesize label;
-(IBAction) fingeron
{
int holdtime;
presstime = CFAbsoluteTimeGetCurrent();
holdtime = (int) (CFAbsoluteTimeGetCurrent() - presstime);
NSString *holdtimetext;
while (holdtime <= 10) {
holdtimetext = [NSString stringWithFormat:@"%d", holdtime];
label.text = holdtimetext;
holdtime = CFAbsoluteTimeGetCurrent() - presstime;
}
presstime = 0;
}
因此label.text为空9秒,而在动作中它显示为“10”
答案 0 :(得分:0)
Crete TImer为此
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(fingerOn) userInfo:nil repeats: YES];
然后使用自己的方法
-(IBAction) fingeron
{
int holdtime;
presstime = CFAbsoluteTimeGetCurrent();
holdtime = (int) (CFAbsoluteTimeGetCurrent() - presstime);
NSString *holdtimetext;
while(holdtime <= 10)
{
holdtimetext = [NSString stringWithFormat:@"%d", holdtime];
label.text = holdtimetext;
holdtime = CFAbsoluteTimeGetCurrent() - presstime;
}
presstime = 0;
}