计算在ios中输入UITextField的文本之间的时差

时间:2015-03-30 10:48:20

标签: ios iphone uitextfield

我有一个UITextField。我想计算两个输入字符之间的时差。例如,如果我输入“a”并等待3秒钟输入第二个字符。如何计算时间。

5 个答案:

答案 0 :(得分:0)

它很简单,使用UItextField委托方法,它在每个char进入字段后调用

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return YES; //we allow the user to enter anything
}

在里面你可以设置你的方法,可以检查以前的输入时间

答案 1 :(得分:0)

将日期实例创建为

NSDate *m_date = [NSDate date];
NSTimeInterval m_secondsBetweenTwoEdits;

- (void)textFieldDidBeginEditing:(UITextField *)textField{  
    //reset the m_date in this method
    m_date = [NSDate date];        
    NSLog(@"textFieldDidBeginEditing");
}

使用UITextField的委托方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // calculate the difference between two edits
    m_secondsBetweenTwoEdits = [[NSDate date] timeIntervalSinceDate:m_date];
    // reset the m_date variable
    m_date = [NSDate date];
    return YES; //we allow the user to enter anything
}

答案 2 :(得分:0)

您可以使用以下逻辑/方法以秒为单位查找时差。

NSDate *characterInputTime ;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange: (NSRange)range replacementString:(NSString *)string {

   NSInteger differenceInSec = 0;

   if(characterInputTime){
     NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
     NSDateComponents *components = [gregorianCalendar components:NSSecondCalendarUnit
                                                    fromDate: characterInputTime
                                                      toDate:[NSDate date]
                                                     options:0];

    differenceInSec = [components second];
   }

 // Do whatever you want to do with Time difference.
   characterInputTime = [NSDate date];

   return YES; //we allow the user to enter anything
}

如果你想在几分钟内检查差异,你也可以使用NSMinuteCalendarUnit。

答案 3 :(得分:0)

首先,您应该通过

将您的类设置为textField的委托
self.myTextfield.delegate = self;

现在您应该从UITextFieldDelegate实现以下功能。另外,在头文件中声明一个名为lastDate的NSDate对象。它不会打印第一个,但它会打印后来的差异。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (lastDate)
    {
        NSLog(@"time diff %f", [[NSDate date] timeIntervalSinceDate:lastDate]);
    }
    lastDate = [NSDate date];
    return YES;
}

答案 4 :(得分:0)

试用此代码:

@property (nonatomic, strong) NSTimer* timer;
@property (nonatomic, assign) NSInteger count;


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if(self.timer){
         // Reset timer if already running
        [self.timer invalidate];
        self.timer=nil;
    }
    NSLog(@"Character entered after %ld seconds",(long)self.count);
    // Reset count & initialize timer again
    self.count=0;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(incrementCount) userInfo:nil repeats:YES];

    return YES;
}

- (void)incrementCount{
    self.count++;
}

确保将文本字段的委托设置为self