我有一个显示数字的标签。用户可以使用两个按钮递增或递减该标签的值。但是,如果用户持有任一按钮,则值将更快或更快地递增或递减。例如,保持1秒快1倍,2秒快2倍,3秒快3倍,依此类推。
这是我尝试但不起作用的原因,因为它不会继续调用该方法,并且您无法看到您在按钮上分配多个UILongGessureRecognizer
。我需要另一种方法。有什么想法吗?
在我的viewDidLoad
中 longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(incrementTwoX)];
longerPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(incrementThreeX)];
[longPressGesture setMinimumPressDuration:1.5];
[longPressGesture setMinimumPressDuration:3];
//add long and longer press gestures to ALL buttons
[weightLeftButton addGestureRecognizer:longPressGesture];
[weightLeftButton addGestureRecognizer:longerPressGesture];
[weightRightButton addGestureRecognizer:longPressGesture];
[weightRightButton addGestureRecognizer:longerPressGesture];
-(void)incrementTwoX{
NSLog(@"Holding");// doesn't get called Im guessing because you can't assign more then one long gesture recognizer to a button
}
-(void)incrementThreeX{
NSLog(@"Holding Longer");
}
- (IBAction)weightRight:(id)sender {
if([weightLabel.text isEqual: @"Not Specified"]) weightLabel.text = 0;
double value = [weightLabel.text doubleValue] + 0.1;
weightLabel.text = [NSString stringWithFormat:@"%f", value];
}
我的进步..
第一次单击按钮并按住时,发生了什么?它只会将标签从未指定更改为0(零)。下次我点击并按住它完美地工作。然后在任何时候它只在每次点击时增加2并且不再保持。你能看出可能出现什么问题吗?使用2个增量来指定更多。点击并按住,无论你持有多长时间,你都不会看到标签的变化。然后,当您松开按钮时(在任何持续时间之后,即使是快速点击),您也会看到标签增量为2
- (void) increment:(UIButton*)currentButton label:(UILabel*)currentLabel{
currentLabel.text = [NSString stringWithFormat: @"%d", _counter++];
// if the button is still pressed, increment again in a little while:
if ( currentButton.state != UIControlStateNormal ){
NSLog(@"incrementing %d", _counter);
CGFloat delay = 1.0/(CGFloat)_counter;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self increment:currentButton label:currentLabel];
});
}
}
- (IBAction)weightRight:(id)sender {
[self increment:weightRightButton label:weightLabel];
}
这就是病日志。第一次点击后,看看它是如何增加的。我放开了91.然后它只增加2,即使if语句只被调用一次。如果我持续很长时间或者只是做一些快速点击,那么看一下后续日志的时间增加2并不重要。发生了什么事?
2014-04-05 11:40:37.960 Stream Craft[1090:60b] incrementing 1
2014-04-05 11:40:38.961 Stream Craft[1090:60b] incrementing 2
2014-04-05 11:40:39.463 Stream Craft[1090:60b] incrementing 3
2014-04-05 11:40:39.798 Stream Craft[1090:60b] incrementing 4
2014-04-05 11:40:40.049 Stream Craft[1090:60b] incrementing 5
2014-04-05 11:40:40.250 Stream Craft[1090:60b] incrementing 6
2014-04-05 11:40:40.419 Stream Craft[1090:60b] incrementing 7
2014-04-05 11:40:40.563 Stream Craft[1090:60b] incrementing 8
2014-04-05 11:40:40.690 Stream Craft[1090:60b] incrementing 9
2014-04-05 11:40:40.803 Stream Craft[1090:60b] incrementing 10
2014-04-05 11:40:40.904 Stream Craft[1090:60b] incrementing 11
2014-04-05 11:40:40.997 Stream Craft[1090:60b] incrementing 12
2014-04-05 11:40:41.081 Stream Craft[1090:60b] incrementing 13
2014-04-05 11:40:41.160 Stream Craft[1090:60b] incrementing 14
2014-04-05 11:40:41.233 Stream Craft[1090:60b] incrementing 15
2014-04-05 11:40:41.301 Stream Craft[1090:60b] incrementing 16
2014-04-05 11:40:41.365 Stream Craft[1090:60b] incrementing 17
2014-04-05 11:40:41.425 Stream Craft[1090:60b] incrementing 18
2014-04-05 11:40:41.481 Stream Craft[1090:60b] incrementing 19
2014-04-05 11:40:41.535 Stream Craft[1090:60b] incrementing 20
2014-04-05 11:40:41.586 Stream Craft[1090:60b] incrementing 21
2014-04-05 11:40:41.636 Stream Craft[1090:60b] incrementing 22
2014-04-05 11:40:41.683 Stream Craft[1090:60b] incrementing 23
2014-04-05 11:40:41.728 Stream Craft[1090:60b] incrementing 24
2014-04-05 11:40:41.771 Stream Craft[1090:60b] incrementing 25
2014-04-05 11:40:41.813 Stream Craft[1090:60b] incrementing 26
2014-04-05 11:40:41.853 Stream Craft[1090:60b] incrementing 27
2014-04-05 11:40:41.891 Stream Craft[1090:60b] incrementing 28
2014-04-05 11:40:41.929 Stream Craft[1090:60b] incrementing 29
2014-04-05 11:40:41.964 Stream Craft[1090:60b] incrementing 30
2014-04-05 11:40:41.999 Stream Craft[1090:60b] incrementing 31
2014-04-05 11:40:42.033 Stream Craft[1090:60b] incrementing 32
2014-04-05 11:40:42.066 Stream Craft[1090:60b] incrementing 33
2014-04-05 11:40:42.097 Stream Craft[1090:60b] incrementing 34
2014-04-05 11:40:42.128 Stream Craft[1090:60b] incrementing 35
2014-04-05 11:40:42.158 Stream Craft[1090:60b] incrementing 36
2014-04-05 11:40:42.187 Stream Craft[1090:60b] incrementing 37
2014-04-05 11:40:42.216 Stream Craft[1090:60b] incrementing 38
2014-04-05 11:40:42.244 Stream Craft[1090:60b] incrementing 39
2014-04-05 11:40:42.271 Stream Craft[1090:60b] incrementing 40
2014-04-05 11:40:42.297 Stream Craft[1090:60b] incrementing 41
2014-04-05 11:40:42.323 Stream Craft[1090:60b] incrementing 42
2014-04-05 11:40:42.348 Stream Craft[1090:60b] incrementing 43
2014-04-05 11:40:42.373 Stream Craft[1090:60b] incrementing 44
2014-04-05 11:40:42.398 Stream Craft[1090:60b] incrementing 45
2014-04-05 11:40:42.420 Stream Craft[1090:60b] incrementing 46
2014-04-05 11:40:42.443 Stream Craft[1090:60b] incrementing 47
2014-04-05 11:40:42.466 Stream Craft[1090:60b] incrementing 48
2014-04-05 11:40:42.489 Stream Craft[1090:60b] incrementing 49
2014-04-05 11:40:42.510 Stream Craft[1090:60b] incrementing 50
2014-04-05 11:40:42.532 Stream Craft[1090:60b] incrementing 51
2014-04-05 11:40:42.553 Stream Craft[1090:60b] incrementing 52
2014-04-05 11:40:42.574 Stream Craft[1090:60b] incrementing 53
2014-04-05 11:40:42.593 Stream Craft[1090:60b] incrementing 54
2014-04-05 11:40:42.613 Stream Craft[1090:60b] incrementing 55
2014-04-05 11:40:42.632 Stream Craft[1090:60b] incrementing 56
2014-04-05 11:40:42.652 Stream Craft[1090:60b] incrementing 57
2014-04-05 11:40:42.671 Stream Craft[1090:60b] incrementing 58
2014-04-05 11:40:42.690 Stream Craft[1090:60b] incrementing 59
2014-04-05 11:40:42.708 Stream Craft[1090:60b] incrementing 60
2014-04-05 11:40:42.725 Stream Craft[1090:60b] incrementing 61
2014-04-05 11:40:42.743 Stream Craft[1090:60b] incrementing 62
2014-04-05 11:40:42.761 Stream Craft[1090:60b] incrementing 63
2014-04-05 11:40:42.777 Stream Craft[1090:60b] incrementing 64
2014-04-05 11:40:42.794 Stream Craft[1090:60b] incrementing 65
2014-04-05 11:40:42.811 Stream Craft[1090:60b] incrementing 66
2014-04-05 11:40:42.827 Stream Craft[1090:60b] incrementing 67
2014-04-05 11:40:42.843 Stream Craft[1090:60b] incrementing 68
2014-04-05 11:40:42.859 Stream Craft[1090:60b] incrementing 69
2014-04-05 11:40:42.875 Stream Craft[1090:60b] incrementing 70
2014-04-05 11:40:42.891 Stream Craft[1090:60b] incrementing 71
2014-04-05 11:40:42.907 Stream Craft[1090:60b] incrementing 72
2014-04-05 11:40:42.922 Stream Craft[1090:60b] incrementing 73
2014-04-05 11:40:42.938 Stream Craft[1090:60b] incrementing 74
2014-04-05 11:40:42.953 Stream Craft[1090:60b] incrementing 75
2014-04-05 11:40:42.968 Stream Craft[1090:60b] incrementing 76
2014-04-05 11:40:42.982 Stream Craft[1090:60b] incrementing 77
2014-04-05 11:40:42.996 Stream Craft[1090:60b] incrementing 78
2014-04-05 11:40:43.009 Stream Craft[1090:60b] incrementing 79
2014-04-05 11:40:43.023 Stream Craft[1090:60b] incrementing 80
2014-04-05 11:40:43.037 Stream Craft[1090:60b] incrementing 81
2014-04-05 11:40:43.051 Stream Craft[1090:60b] incrementing 82
2014-04-05 11:40:43.064 Stream Craft[1090:60b] incrementing 83
2014-04-05 11:40:43.077 Stream Craft[1090:60b] incrementing 84
2014-04-05 11:40:43.090 Stream Craft[1090:60b] incrementing 85
2014-04-05 11:40:43.103 Stream Craft[1090:60b] incrementing 86
2014-04-05 11:40:43.116 Stream Craft[1090:60b] incrementing 87
2014-04-05 11:40:43.119 Stream Craft[1090:60b] incrementing 88
2014-04-05 11:40:47.107 Stream Craft[1090:60b] incrementing 91
2014-04-05 11:40:52.233 Stream Craft[1090:60b] incrementing 93
2014-04-05 11:40:55.157 Stream Craft[1090:60b] Ad did load
2014-04-05 11:40:56.271 Stream Craft[1090:60b] incrementing 95
2014-04-05 11:41:00.233 Stream Craft[1090:60b] incrementing 97
2014-04-05 11:41:03.849 Stream Craft[1090:60b] incrementing 99
2014-04-05 11:41:10.193 Stream Craft[1090:60b] incrementing 101
2014-04-05 11:41:11.319 Stream Craft[1090:60b] incrementing 103
2014-04-05 11:41:12.122 Stream Craft[1090:60b] incrementing 105
2014-04-05 11:41:12.373 Stream Craft[1090:60b] incrementing 107
2014-04-05 11:41:12.544 Stream Craft[1090:60b] incrementing 109
2014-04-05 11:41:12.700 Stream Craft[1090:60b] incrementing 111
2014-04-05 11:41:12.802 Stream Craft[1090:60b] incrementing 113
这也是我的按钮和标签的设置方式......
@property (strong, nonatomic) IBOutlet UILabel *weightLabel;
@property (strong, nonatomic) IBOutlet UIButton *weightRightButton;
@property (strong, nonatomic) IBOutlet UIButton *weightLeftButton;
如果重要的话,发送的事件也是“触摸内部”我的按钮?
答案 0 :(得分:5)
编辑更新了稍微好一点的例子。
如果你已经有了UIButton,我就不会弄乱手势识别器了。您需要做的就是不断检查按钮是否仍然按下,以及是否增加了计数器。您可以减少检查之间的时间,以便计数器速度从慢到快,如您所述。
这里的技巧是使用GCD通过dispatch_after()在延迟后派遣一些工作。
这是一个完整的解决方案。如果您需要自定义加速时间(正如您在问题中讨论的那样),只需记录按钮首次按下的时间,并根据经过的总时间计算下一个增量的时间。
@interface TSViewController ()
{
IBOutlet UILabel* _label;
IBOutlet UIButton* _button;
int _counter;
NSDate* _start;
}
@end
@implementation TSViewController
- (void) increment
{
// if the button is still pressed, increment again in a little while:
if ( _button.state != UIControlStateNormal )
{
_label.text = [NSString stringWithFormat: @"%d", _counter++];
CGFloat elapsed = MAX(1, fabs( [_start timeIntervalSinceNow] ) );
CGFloat delay = 0.9/elapsed;
NSLog( @"%.3lf, delay: %.3lf", elapsed , delay);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self increment];
});
}
}
- (IBAction) touchDown:(UIButton*)sender
{
_start = [NSDate date];
[self increment];
}
@end
答案 1 :(得分:3)
首先,无需初始化UILongPressGestureRecognizer两次,您可以尝试以下方法: -
通过这种方式,您可以继续提高增值/减量的速度。
答案 2 :(得分:0)
尝试使用UIStepper
控件。
将此功能连接到其ValueChanged
事件:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// `nameLabel` defined in .h file
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(8.0f, 100.0f, 135.0f, 36.0f)];
nameLabel.textColor = [UIColor whiteColor];
[self.view addSubview:nameLabel];
}
-(IBAction)valChanged:(UIStepper *)sender
{
[nameLabel setText:[NSString stringWithFormat:@"%0.1f", ((UIStepper *)sender).value]];
}
实施此代码并按住此步进器+
几秒钟。