2 touch拖动问题

问题描述:

我用下面的代码来处理我的应用程序中的触摸,我用touchesBegan循环在同一时间处理2个触摸,但问题是如果用户用2个手指拖动一个工作我可以正确地做2件作品吗?2 touch拖动问题

代码:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 

touchesMoved代码:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    MainViewController *Note = [[MainViewController alloc] init]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 
    NSLog(@"pointx: %f pointy:%f", location.x, location.y); 

    if(CGRectContainsPoint(b_do.frame , location)) 
    { 
     if (![b_do isHighlighted]){ 
      [Note playNote:[b_do currentTitle] type:@"wav"]; 
      [self b_do]; 
      [b_do setHighlighted:YES]; 
      NSLog(@"Doooo"); 
     } 
    }else { 
     [b_do setHighlighted:NO]; 
     NSLog(@"Not Do"); 
    } 

    if(CGRectContainsPoint(b_re.frame, location)) 
    { 
     if (!b_re.isHighlighted){ 
      [self b_re]; 
      [Note playNote:[b_re currentTitle] type:@"wav"]; 
      [b_re setHighlighted:YES]; 
     } 
    }else { 
     [b_re setHighlighted:NO]; 
    } 
    if(CGRectContainsPoint(b_me.frame, location)) 
    { 
     if (!b_me.isHighlighted){ 
      [self b_me]; 
      [Note playNote:[b_me currentTitle] type:@"wav"]; 
      [b_me setHighlighted:YES]; 
     } 
    }else { 
     [b_me setHighlighted:NO]; 
    } 
    if (CGRectContainsPoint(b_fa.frame, location)) 
    { 
     if (!b_fa.isHighlighted){ 
      [self b_fa]; 
      [Note playNote:[b_fa currentTitle] type:@"wav"]; 
      [b_fa setHighlighted:YES]; 
     } 
    }else { 
     [b_fa setHighlighted:NO]; 
    } 
    if (CGRectContainsPoint(b_sol.frame, location)) 
    { 
     if (!b_sol.isHighlighted){ 
      [self b_sol]; 
      [Note playNote:[b_sol currentTitle] type:@"wav"]; 
      [b_sol setHighlighted:YES]; 
     } 
    }else { 
     [b_sol setHighlighted:NO]; 
    } 
    if (CGRectContainsPoint(b_la.frame, location)) 
    { 
     if (!b_la.isHighlighted){ 
      [self b_la]; 
      [Note playNote:[b_la currentTitle] type:@"wav"]; 
      [b_la setHighlighted:YES]; 
     } 
    }else { 
     [b_la setHighlighted:NO]; 
    } 
    if (CGRectContainsPoint(b_ci.frame, location)) 
    { 
     if (!b_ci.isHighlighted){ 
      [self b_la]; 
      [Note playNote:[b_ci currentTitle] type:@"wav"]; 
      [b_ci setHighlighted:YES]; 
     } 
    }else { 
     [b_ci setHighlighted:NO]; 
    } 
    if (CGRectContainsPoint(b_doo.frame, location)) 
    { 
     if (!b_doo.isHighlighted){ 
      [self b_doo]; 
      [Note playNote:[b_doo currentTitle] type:@"wav"]; 
      [b_doo setHighlighted:YES]; 
     } 
    }else { 
     [b_doo setHighlighted:NO]; 
    } 
} 

如果你是在3.2或更高版本,我建议你更改使用GestureRecognizer这是很多更容易使用和理解比touchesBegan的东西。