iPhone: detect two finger touch

后端 未结 3 1472
独厮守ぢ
独厮守ぢ 2021-02-15 14:28

I need to detect two finger touch event. If i touch the screen with two fingers on same time, so everthing is ok. Just using such code:

- (void)touchesBegan:(NSS         


        
相关标签:
3条回答
  • 2021-02-15 15:12

    Make sure the UIView has multipleTouchEnabled=YES, the default is NO.

    Edit:I see what the problem is. In touchesBegan:withEvent:, you will only get the new touches. You don't get all the active touches. It is very unlikely, if at all possible, that you will get more than one touch to begin at the same time. To check is there is more than one active touch int touchesBegan: try this:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    
        if ([[event touchesForView:self] count] > 1) {
            NSLog(@"%d active touches",[[event touchesForView:self.view] count]) ;
    
    
        }
        [super touchesBegan:touches withEvent:event] ;
    }
    
    0 讨论(0)
  • 2021-02-15 15:19
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch1, *touch2;
        CGPoint    location1, location2;
    
        NSSet *allTouches = [event allTouches];
        touch1 = [[allTouches allObjects] objectAtIndex:0];
        location1 = [touch1 locationInView:self];
    
        if([allTouches count] > 1)
        {
            touch2 = [[allTouches allObjects] objectAtIndex:1];
            location2 = [touch2 locationInView:self];
        }
    }
    
    0 讨论(0)
  • 2021-02-15 15:25

    What is the real object that is receiving the event? That object may get your touch event and not the base UIResponder.

    For example if you are inside a UIScroolView then you can get the sec touch in:
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;

    0 讨论(0)
提交回复
热议问题