我一直在努力做点什么而且我无法帮助我。我有一个例如3的变量,我希望它在用户触摸屏幕时增加并保持它。例如,如果他只点击屏幕,变量将是3,如果他保持2秒变量将是5,如果他持有5秒,变量将是10.类似的东西,用户持有的越多更多变量将增加。请帮帮我!
答案 0 :(得分:0)
只需在同一视图中添加多个手势识别器,即可实现您所描述的内容。您可能正在寻找的是UIGestureRecognizerDelegate协议中的gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:方法。
此方法允许同时识别多个手势,这不是手势识别器的默认行为。
以下代码完全符合您的描述。
@interface ViewController () <UIGestureRecognizerDelegate>
@property (nonatomic) NSUInteger variable;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.variable = 0;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(firstMethod)];
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(secondMethod)];
UILongPressGestureRecognizer *secondPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(thirdMethod)];
press.minimumPressDuration = 2;
secondPress.minimumPressDuration = 5;
press.delegate = self;
secondPress.delegate = self;
[self.view addGestureRecognizer:tap];
[self.view addGestureRecognizer:press];
[self.view addGestureRecognizer:secondPress];
}
- (void)firstMethod
{
self.variable = 2;
NSLog(@"%lu", (unsigned long)self.variable);
}
- (void)secondMethod
{
self.variable = 5;
NSLog(@"%lu", (unsigned long)self.variable);
}
- (void)thirdMethod
{
self.variable = 10;
NSLog(@"%lu", (unsigned long)self.variable);
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
@end