0

每个人!我需要同时检测不同图像视图上的 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];
}

谢谢你的帮助))

4

4 回答 4

1

你可能想看看UIGestureRecognizerDelegate然后方法:

gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
于 2012-11-19T13:13:06.740 回答
0

试试这个 :

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  UITouch *touch = [touches anyObject];
      if ([touch view] == firstimage    ) { // Do Something}
      if ([touch view] == secondimage    ) { // Do Something}
 }
于 2012-11-19T12:55:51.987 回答
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];
}
于 2012-11-21T15:20:38.233 回答
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;
}

然后您识别两次触摸的位置,并决定您要做什么;

于 2012-11-21T15:41:05.103 回答