大家好!我需要在同一时间检测不同图像视图上的2个触摸。因此,当用户同时触摸两个图像视图时,我需要启动计时器。并在触摸结束时停止计时器。图像视图正在屏幕上移动。使用一个图像视图时没有问题。你有什么想法吗?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches)
for (UITouch *touch2 in allTouches)
{
CGPoint location = [touch locationInView:touch.view];
CGPoint location2 = [touch2 locationInView:touch2.view];
if ([touchArea2.layer.presentationLayer hitTest:location2]) {
touchArea2.image = [UIImage imageNamed:@"TouchAreaTouched"];
}
if ([touchArea.layer.presentationLayer hitTest:location]) {
touchArea.image = [UIImage imageNamed:@"TouchAreaTouched"];
timerTouch = 10;
touchTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(randomVoid) userInfo:nil repeats:YES];
} else if (![touchArea.layer.presentationLayer hitTest:location]){
[touchTimer invalidate];
touchTimer = nil;
} }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches)
for (UITouch *touch2 in allTouches) {
CGPoint location = [touch locationInView:touch.view];
CGPoint location2 = [touch2 locationInView:touch2.view];
if (![touchArea.layer.presentationLayer hitTest:location]){
touchArea2.image = [UIImage imageNamed:@"TouchArea"];
touchArea.image = [UIImage imageNamed:@"TouchArea"];
[touchTimer invalidate];
touchTimer = nil;
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
touchArea.image = [UIImage imageNamed:@"TouchArea"];
[touchTimer invalidate];
}
感谢您的帮助))
答案 0 :(得分:1)
您可能需要查看UIGestureRecognizerDelegate
,然后查看方法:
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
答案 1 :(得分:0)
试试这个:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if ([touch view] == firstimage ) { // Do Something}
if ([touch view] == secondimage ) { // Do Something}
}
答案 2 :(得分:0)
我经常考虑这个问题而且我找到了如何做到的方法。 这是我的代码:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches)
for (UITouch *touch2 in allTouches)
{
CGPoint location = [touch locationInView:touch.view];
CGPoint location2 = [touch2 locationInView:touch2.view];
if ([touchArea2.layer.presentationLayer hitTest:location]) {
touchArea2.image = [UIImage imageNamed:@"TouchAreaTouched"];
}
if ([touchArea.layer.presentationLayer hitTest:location]) {
touchArea.image = [UIImage imageNamed:@"TouchAreaTouched"];
if ([touchArea2.layer.presentationLayer hitTest:location2]) {
touchArea2.image = [UIImage imageNamed:@"TouchAreaTouched"];
timerTouch = 10;
touchTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(randomVoid) userInfo:nil repeats:YES];
} }}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches)
{
CGPoint location = [touch locationInView:touch.view];
if (![touchArea.layer.presentationLayer hitTest:location]&&![touchArea2.layer.presentationLayer hitTest:location]) {
touchArea.image = [UIImage imageNamed:@"TouchArea"];
touchArea2.image = [UIImage imageNamed:@"TouchArea"];
[touchTimer invalidate];
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
touchArea.image = [UIImage imageNamed:@"TouchArea"];
touchArea2.image = [UIImage imageNamed:@"TouchArea"];
[touchTimer invalidate];
}
答案 3 :(得分:0)
UIView * view = [[UIView alloc] init];
UITapGestureRecognizer * tap = nil;
tap.numberOfTouchesRequired = 2;
tap.delegate = self;
[view addGestureRecognizer:tap];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
然后你识别两个触摸的位置,并决定你做什么;