0

我正在尝试计算用户将手指按住按钮的时间,并将此时间以秒为单位显示为 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

我的m文件

#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 秒,并且在操作的 and 中显示“10”

4

1 回答 1

0

克里特岛计时器为此

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;
}
于 2012-05-31T11:48:40.423 回答