获得一,二和一三个水龙头在同一个View Controller中工作

时间:2013-09-03 10:23:11

标签: iphone ios objective-c uigesturerecognizer

我正在使用UITapGestureRecognizer并尝试使用同一个ViewController进行不同的点按,但只需点击一下即可失败

我试图找到解决方案,但无法将其集成到我的测试代码中。我非常感谢一些帮助,所以我可以从中学习。

这是我用来测试的代码:

.h文件:

@interface ViewController : UIViewController <UIGestureRecognizerDelegate>

.m文件:

@interface ViewController ()

@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer3;
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer2;
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer1;

@end

@implementation ViewController

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

return YES;

}

- (void)viewDidLoad
{
[super viewDidLoad];


self.view.backgroundColor = [UIColor greenColor];

// Create Tap Gesture Recognizer

//==1
self.tapGestureRecognizer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle1Taps:)];
self.tapGestureRecognizer1.numberOfTouchesRequired = 1;
self.tapGestureRecognizer1.numberOfTapsRequired = 1;

//==2
self.tapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle2Taps:)];
self.tapGestureRecognizer2.numberOfTouchesRequired = 1;
self.tapGestureRecognizer3.numberOfTapsRequired = 2;

//== #3
self.tapGestureRecognizer3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle3Taps:)];
self.tapGestureRecognizer3.numberOfTouchesRequired = 1;
self.tapGestureRecognizer3.numberOfTapsRequired = 3;


[self.tapGestureRecognizer1 requireGestureRecognizerToFail:self.tapGestureRecognizer2];
[self.tapGestureRecognizer1 requireGestureRecognizerToFail:self.tapGestureRecognizer3];
[self.tapGestureRecognizer2 requireGestureRecognizerToFail:self.tapGestureRecognizer3];

// Add gestures
[self.view addGestureRecognizer:self.tapGestureRecognizer1];
[self.view addGestureRecognizer:self.tapGestureRecognizer2];
[self.view addGestureRecognizer:self.tapGestureRecognizer3];

}

- (void)handle1Taps:(UITapGestureRecognizer *)paramSender {
NSLog(@"handle-1-Taps");

NSUInteger touchCounter = 0;

for (touchCounter = 0; touchCounter < paramSender.numberOfTouchesRequired; touchCounter++) {

    CGPoint touchPoint = [paramSender locationOfTouch:touchCounter inView:paramSender.view];

    NSLog(@">>1 x TAP<<");


}

}

- (void)handle2Taps:(UITapGestureRecognizer *)paramSender {
NSLog(@"handle-2-Taps");
NSUInteger touchCounter = 0;

for (touchCounter = 0; touchCounter < paramSender.numberOfTouchesRequired; touchCounter++) {

    CGPoint touchPoint = [paramSender locationOfTouch:touchCounter inView:paramSender.view];

    NSLog(@">>2 x TAP<<");


}

}

- (void)handle3Taps:(UITapGestureRecognizer *)paramSender {
NSLog(@"handle-3-Taps");
NSUInteger touchCounter = 0;

for (touchCounter = 0; touchCounter < paramSender.numberOfTouchesRequired; touchCounter++) {

    CGPoint touchPoint = [paramSender locationOfTouch:touchCounter inView:paramSender.view];

    NSLog(@">>3 x TAP<<");


}

}

@end

以下是与VC首次互动时的结果:

 - 1 x tap = >>2 x TAP<<
 - 2 x tap = >>2 x TAP<<
 - 3 x tap = >>3 x TAP<<

5 个答案:

答案 0 :(得分:2)

<强>哎 检查一下: 最后一行是错误的。

self.tapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle2Taps:)];
self.tapGestureRecognizer2.numberOfTouchesRequired = 1;
self.tapGestureRecognizer3.numberOfTapsRequired = 2;

应该是

self.tapGestureRecognizer2.numberOfTapsRequired = 2;

希望这会对你有所帮助。

答案 1 :(得分:0)

试试这个

评论此声明,该声明出现在您的代码中的三个位置。

self.tapGestureRecognizer1.numberOfTouchesRequired = 1;

还有一个问题,请更换

self.tapGestureRecognizer3.numberOfTapsRequired = 2;

self.tapGestureRecognizer2.numberOfTapsRequired = 2;

因为tapGestureRecognizer2需要等于2的抽头数

答案 2 :(得分:0)

看起来你的逻辑在下面的bold_highlighted代码行中存在问题!

  

- (void)viewDidLoad {[super viewDidLoad];

     

self.view.backgroundColor = [UIColor greenColor];

     

//创建Tap手势识别器

     

// == 1 self.tapGestureRecognizer1 = [[UITapGestureRecognizer]   alloc] initWithTarget:self action:@selector(handle1Taps :)];   self.tapGestureRecognizer1.numberOfTouchesRequired = 1;   self.tapGestureRecognizer1.numberOfTapsRequired = 1;

     

// == 2 self.tapGestureRecognizer2 = [[UITapGestureRecognizer]   alloc] initWithTarget:self action:@selector(handle2Taps :)];   self.tapGestureRecognizer2.numberOfTouchesRequired = 1;    self.tapGestureRecognizer3.numberOfTapsRequired = 2;

     

// ==#3 self.tapGestureRecognizer3 = [[UITapGestureRecognizer]   alloc] initWithTarget:self action:@selector(handle3Taps :)];   self.tapGestureRecognizer3.numberOfTouchesRequired = 1;   self.tapGestureRecognizer3.numberOfTapsRequired = 3;

     

[self.tapGestureRecognizer1 requireGestureRecognizerToFail:self.tapGestureRecognizer2];   [self.tapGestureRecognizer1   requireGestureRecognizerToFail:self.tapGestureRecognizer3];   [self.tapGestureRecognizer2   requireGestureRecognizerToFail:self.tapGestureRecognizer3];

     

//添加手势[self.view   addGestureRecognizer:self.tapGestureRecognizer1]; [self.view   addGestureRecognizer:self.tapGestureRecognizer2]; [self.view   addGestureRecognizer:self.tapGestureRecognizer3];

     

}

答案 3 :(得分:0)

以编程方式(以某种风格)进行。

UILongPressGestureRecognizer *longPress;
int idx;

for (idx = 1; idx <= 3; idx++) {
    longPress = [[UILongPressGestureRecognizer alloc]
                                           initWithTarget:self action:@selector(buttonLongPressed:)];
    longPress.minimumPressDuration = min_press_duration;
    longPress.numberOfTapsRequired = 0;
    longPress.numberOfTouchesRequired = idx;
    [button addGestureRecognizer:longPress];
    [longPress release];
}

答案 4 :(得分:-1)

[NSObject cancelPreviousPerformRequestsWithTarget:self];添加到handleDoubleTap和handlelTripleTap方法。这将取消先前操作的执行。