所以这是一个奇怪的问题。我有一个响应 touchesMoved:
& touchesBegan:
的 UIImageView,它工作得很好。我遇到的主要问题是,如果您将手指放在 image1 上,然后手指仍然放在 image1 上,您会用另一根手指按下 image2。
反过来,这会再次触发 image1。不希望发生这种情况。我希望在同时按下两个图像时能够使用多点触控,而不是一遍又一遍地触发它们。我必须添加一些东西来阻止这种情况发生。
代码:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches)
{
CGPoint location = [touch locationInView:touch.view];
if(CGRectContainsPoint(image1.frame, location) && lastButton != image1) {
//Play Sound
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1"
ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
, &soundID);
AudioServicesPlaySystemSound (soundID);
//
lastButton = image1;
}
if(CGRectContainsPoint(image2.frame, location) && lastButton != image2) {
//Play Sound
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2"
ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
, &soundID);
AudioServicesPlaySystemSound (soundID);
//
lastButton = image2;
}
}
对于 touchesMoved:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if(CGRectContainsPoint(image1.frame, location) && lastButton != image1) {
//Play Sound
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1"
ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
, &soundID);
AudioServicesPlaySystemSound (soundID);
//
lastButton = image1;
}
if(CGRectContainsPoint(image2.frame, location) && lastButton != image2) {
//Play Sound
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2"
ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
, &soundID);
AudioServicesPlaySystemSound (soundID);
//
lastButton = image2;
}
}
最后是决赛:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
lastButton = nil;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}
所以我只需要这样做,如果我按住其中一个图像,然后按下另一个图像,然后再次松开它,然后再次按下它,它就会停止触发我按下的第一个图像。
最佳答案
所有涉及的 View 都将收到所有触摸(在 Jason 提到的 NSSet
中)。由每个 View (即由您决定)选择您感兴趣的触摸。因此您必须检查每个触摸是否在 View 的范围内,并尝试将触摸与最后一个触摸相关联。例如,由于您在一组中进行了接触,因此您不能依赖它们的顺序来计算它们移动了多少。您必须保存最后一次触摸的坐标并选择最接近的触摸并假设它是移动的同一根手指。
关于iphone - touchesBegan 和 touchesMoved,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3337289/