所以这里有个奇怪的问题。我有一个响应touchesMoved: & touchesBegan:的UIImageView,它工作得很好。我遇到的主要问题是,如果你把手指放在image1上,然后把手指放在image1上,你就会用另一根手指按image2。
我希望能够在同时推送两个图像时使用多点触摸,而不是一遍又一遍地发射它们。我必须添加一些东西来阻止这种情况的发生。
代码:
-(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];
}所以我只需要这样做,如果我按住其中一个图像,然后按下另一个图像,然后再松开它,再按下它,它就会停止发射我按下的第一个图像。
发布于 2010-07-27 18:51:49
所有涉及到的视图都将接受所有的处理(在Jason提到的NSSet中)。这取决于每个视图(也就是说,取决于您)选择您感兴趣的视图。因此,您必须检查每个触摸是否在视图的限制内,并尝试将触摸与上一次接收的触摸相关联。例如,由于你在一个集合中获得了触点,所以你不能依靠它们的顺序来计算它们移动了多少。您必须保存最后一次触摸的坐标,并选择最近的触摸,并假设移动的是同一个手指。
发布于 2010-07-27 01:33:49
使用传递到touchesBegan:方法中的touches NSSet,而不是来自事件的allTouches:。
https://stackoverflow.com/questions/3337289
复制相似问题