Iphone 同时检测2次触摸
各位!!我需要检测两个不同的图像视图在同一时间接触。所以当用户同时触摸两个图像视图时,我需要启动计时器。当触摸结束时停止定时器。图像视图在屏幕上移动。使用一个图像视图时没有问题。你有什么想法吗Iphone 同时检测2次触摸,iphone,ios,xcode,ipad,ios6,Iphone,Ios,Xcode,Ipad,Ios6,各位!!我需要检测两个不同的图像视图在同一时间接触。所以当用户同时触摸两个图像视图时,我需要启动计时器。当触摸结束时停止定时器。图像视图在屏幕上移动。使用一个图像视图时没有问题。你有什么想法吗 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSSet *allTouches = [event allTouches]; for (UITouch *touch in allTouches)
-(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];
}
谢谢你的帮助)试试这个:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if ([touch view] == firstimage ) { // Do Something}
if ([touch view] == secondimage ) { // Do Something}
}
您可能需要查看
UIgestureRecognitizerDelegate
,然后查看方法:
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
我对这个问题考虑了很多,我找到了解决的方法。 这是我的密码:
-(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];
}
然后你识别出两次触摸的位置,并决定你要做什么 “同时两次触摸”-一秒钟的延迟是“同时”吗?100毫秒怎么样?1分钟怎么样?我试过了,但是图像视图是动画的。这就是为什么我选择使用-(void)touchsmoved:(NSSet*)touchswithevent:(UIEvent*)event我很抱歉不能帮助您,那么我认为当您使用touch with event时,为什么不使用一个清晰的视图,即两个图像视图的super视图,然后使用位置识别器二次触摸;希望能帮助你
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;
}