检测用户点击

时间:2016-09-29 04:42:54

标签: objective-c uitapgesturerecognizer

我想使用点击手势识别器来检测用户点击。我对此没有太多了解。任何人都可以帮助我.Tap手势。

我想设置最大计数,我想检测用户点击计数是否相等我想要执行某些操作。请帮我这样做。

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
[self.imageforcapture addGestureRecognizer:tapGesture];

以上代码是茶手势识别器的简单示例。

提前致谢!!!!

3 个答案:

答案 0 :(得分:1)

初始化方法:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
[self.imageforcapture addGestureRecognizer:tapGesture];

处理方法:

- (void)handleTapGesture:(UITapGestureRecognizer*)sender {
    i=i+1;
    if(i==10)
    {
        //Prforme task here
    }
}

i声明为全局变量

答案 1 :(得分:0)

使用此:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)];
    tapRecognizer.numberOfTapsRequired = 1;
    tapRecognizer.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:tapRecognizer];

-(void)foundTap:(UITapGestureRecognizer *)recognizer
{

    // Your code goes here

}

答案 2 :(得分:0)

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;

self.imageforcapture.userInteractionEnabled = YES;
[self.imageforcapture addGestureRecognizer:tapGesture];

首先,您需要通过添加上面的行来允许imageView接收触摸。

定义全局变量i。

@property NSInteger i;

在viewDidLoad中将i的值设置为0

self.i = 0;

之后,您需要处理将跟踪点击的全局i变量。

- (void)handleTapGesture:(UITapGestureRecognizer*)sender {
self.i++;
if (self.i == 10) {
//Perform your expected behaviour
}
}