首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >touchesBegan和touchesMoved

touchesBegan和touchesMoved
EN

Stack Overflow用户
提问于 2010-07-27 01:23:50
回答 2查看 25.3K关注 0票数 6

所以这里有个奇怪的问题。我有一个响应touchesMoved: & touchesBegan:的UIImageView,它工作得很好。我遇到的主要问题是,如果你把手指放在image1上,然后把手指放在image1上,你就会用另一根手指按image2。

我希望能够在同时推送两个图像时使用多点触摸,而不是一遍又一遍地发射它们。我必须添加一些东西来阻止这种情况的发生。

代码:

代码语言:javascript
复制
    -(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:

代码语言:javascript
复制
-(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;
        }

    }

代码语言:javascript
复制
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    lastButton = nil;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesEnded:touches withEvent:event];
}

所以我只需要这样做,如果我按住其中一个图像,然后按下另一个图像,然后再松开它,再按下它,它就会停止发射我按下的第一个图像。

EN

回答 2

Stack Overflow用户

发布于 2010-07-27 18:51:49

所有涉及到的视图都将接受所有的处理(在Jason提到的NSSet中)。这取决于每个视图(也就是说,取决于您)选择您感兴趣的视图。因此,您必须检查每个触摸是否在视图的限制内,并尝试将触摸与上一次接收的触摸相关联。例如,由于你在一个集合中获得了触点,所以你不能依靠它们的顺序来计算它们移动了多少。您必须保存最后一次触摸的坐标,并选择最近的触摸,并假设移动的是同一个手指。

票数 1
EN

Stack Overflow用户

发布于 2010-07-27 01:33:49

使用传递到touchesBegan:方法中的touches NSSet,而不是来自事件的allTouches:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3337289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档