我正在尝试实现一些代码来衡量用户触摸屏幕的时间,触摸之间的时间和触摸量。用户触摸屏幕的时间非常好。但是,触摸量始终等于1,触摸之间的时间总是nan
。我在这里做错了什么?
首先我做:
@property (nonatomic, strong) NSDate *touchStart;
@property (nonatomic, strong) NSDate *touchFinish;
@property (nonatomic) NSTimeInterval touchTime;
@property (nonatomic, strong) NSDate *untouchStart;
@property (nonatomic, strong) NSDate *untouchFinish;
@property (nonatomic) NSTimeInterval untouchTime;
@property (nonatomic, assign) NSInteger numberOfTouched;
然后:
- (void)viewDidLoad
{
self.untouchStart = [NSDate date];
self.numberOfTouched = 0;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
self.backgroundColorBackup = self.backgroundColor;
self.backgroundColor = self.tintColor;
UIColor *textColor = ([self.backgroundColorBackup isEqual:[UIColor clearColor]] ?
[self.class averageContentColor] : self.backgroundColorBackup);
self.numberLabel.textColor = textColor;
self.lettersLabel.textColor = textColor;
self.touchStart = [NSDate date];
self.untouchFinish = [NSDate date];
self.untouchTime = [self.untouchFinish timeIntervalSinceDate:self.untouchStart];
NSLog(@"This button was touched after: %f", self.untouchTime);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[self resetHighlight];
self.numberOfTouched = self.numberOfTouched + 1;
self.touchFinish = [NSDate date];
self.touchTime = [self.touchFinish timeIntervalSinceDate:self.touchStart];
NSLog(@"This button was touched for: %f", self.touchTime);
self.untouchStart = [NSDate date];
NSLog(@"The number of buttons touched is: %ld", (long)self.numberOfTouched);
}