Multitouch touchesBegan

Discussion in 'iOS Programming' started by nickculbertson, Jan 17, 2011.

  1. macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #1
    I've had a great deal of trouble getting multitouch to work properly in touchesBegan. I've found plenty of tuts online but none have worked for me so far. I'm trying to make a guitar type app. I can get touchesMoved to fire properly but touching down on the notes only recognizes the first touch. What have I missed?

    Code:
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    	//NSSet *touchy = [touches setByAddingObjectsFromSet:[event allTouches]];
    	//NSLog(@"Adding %d touches.", [[touchy allObjects] count]);
    	//NSUInteger *touch = [[touches anyObject] tapCount];
    	////for (UITouch *touch in touches){
    	//Touch *touch = [touches anyObject];
    	//[self setMultipleTouchEnabled:YES];
    	//NSSet *touches = [[event allTouches] anyObject];
    	//NSMutableSet *currentTouches = [[[event touchesForView:self]mutableCopy] autorelease];
    	//[currentTouches minusSet:touches];
    	
    	//int nb = [touch count];
    	
    UITouch *touch = [[event allTouches] anyObject];
    	
    	CGPoint location1 = [touch locationInView:touch.view];
    	CGPoint location1b = [touch locationInView:touch.view];
    	CGPoint location1c = [touch locationInView:touch.view];
    	CGPoint location1d = [touch locationInView:touch.view];
    		
    	//if ([touches count] == 1) 
    	//for (UITouch *touch in touches){
    	//if ([currentTouches count] >0) {
    		
    	NSLog(@"1");
    	
        if(CGRectContainsPoint(pianoButton1.frame, location1)) 
        {	if (label1.text == @"0")
    		{[pianoButton1 setHighlighted:YES];
    		NSString *path = [[NSBundle mainBundle] pathForResource:@"uku G" ofType:@"wav"];
    		AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic = myMusic11;
    			myMusic.delegate = self; 
    			[myMusic play];}
    	else if (label1.text == @"1"){[pianoButton1 setHighlighted:YES];
    		NSString *path = [[NSBundle mainBundle] pathForResource:@"uku G#" ofType:@"wav"];
    		AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    		self.myMusic = myMusic11;
    		myMusic.delegate = self; 
    		[myMusic play];}
    		if (label1.text == @"2")
    		{[pianoButton1 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku A" ofType:@"wav"];
    			AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic = myMusic11;
    			myMusic.delegate = self; 
    			[myMusic play];}
    		else if (label1.text == @"3"){[pianoButton1 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku A#" ofType:@"wav"];
    			AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic = myMusic11;
    			myMusic.delegate = self; 
    			[myMusic play];}
    		if (label1.text == @"4")
    		{[pianoButton1 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku B" ofType:@"wav"];
    			AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic = myMusic11;
    			myMusic.delegate = self; 
    			[myMusic play];}
    		else if (label1.text == @"5"){[pianoButton1 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku 2C" ofType:@"wav"];
    			AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic = myMusic11;
    			myMusic.delegate = self; 
    			[myMusic play];}
    	}else {
            [pianoButton1 setHighlighted:NO];
        }
        if(CGRectContainsPoint(pianoButton2.frame, location1b)) 
        {	if (label2.text == @"0")
    		{[pianoButton2 setHighlighted:YES];
    		NSString *path = [[NSBundle mainBundle] pathForResource:@"uku C" ofType:@"wav"];
    		AVAudioPlayer * myMusic22=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    		self.myMusic2 = myMusic22;
    			myMusic2.delegate = self; 
    			[myMusic2 play];
    		}
    		if (label2.text == @"1")
    		{[pianoButton2 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku C#" ofType:@"wav"];
    			AVAudioPlayer * myMusic22=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic2 = myMusic22;
    			myMusic2.delegate = self; 
    			[myMusic2 play];
    		}
    		if (label2.text == @"2")
    		{[pianoButton2 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku D" ofType:@"wav"];
    			AVAudioPlayer * myMusic22=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic2 = myMusic22;
    			myMusic2.delegate = self; 
    			[myMusic2 play];
    		}
    		if (label2.text == @"3")
    		{[pianoButton2 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku D#" ofType:@"wav"];
    			AVAudioPlayer * myMusic22=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic2 = myMusic22;
    			myMusic2.delegate = self; 
    			[myMusic2 play];
    		}
    		if (label2.text == @"4")
    		{[pianoButton2 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku E" ofType:@"wav"];
    			AVAudioPlayer * myMusic22=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic2 = myMusic22;
    			myMusic2.delegate = self;
    			[myMusic2 play];
    		}
    		if (label2.text == @"5")
    		{[pianoButton2 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku F" ofType:@"wav"];
    			AVAudioPlayer * myMusic22=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic2 = myMusic22;
    			myMusic2.delegate = self; 
    			[myMusic2 play];
    		}
    	}else {
            [pianoButton2 setHighlighted:NO];
        }
        if(CGRectContainsPoint(pianoButton3.frame, location1c))
        {   if (label3.text == @"0")
    	{[pianoButton3 setHighlighted:YES];
    		NSString *path = [[NSBundle mainBundle] pathForResource:@"uku E" ofType:@"wav"];
    		AVAudioPlayer * myMusic33=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    		self.myMusic3 = myMusic33;
    		myMusic3.delegate = self; 
    		[myMusic3 play];}
    	else if (label3.text == @"1"){[pianoButton3 setHighlighted:YES];
    		NSString *path = [[NSBundle mainBundle] pathForResource:@"uku F" ofType:@"wav"];
    		AVAudioPlayer * myMusic33=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    		self.myMusic3 = myMusic33;
    		myMusic3.delegate = self; 
    		[myMusic3 play];}
    		if (label3.text == @"2")
    		{[pianoButton3 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku F#" ofType:@"wav"];
    			AVAudioPlayer * myMusic33=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic3 = myMusic33;
    			myMusic3.delegate = self; 
    			[myMusic3 play];}
    		else if (label3.text == @"3"){[pianoButton3 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku G" ofType:@"wav"];
    			AVAudioPlayer * myMusic33=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic3 = myMusic33;
    			myMusic3.delegate = self; 
    			[myMusic3 play];}
    		if (label3.text == @"4")
    		{[pianoButton3 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku G#" ofType:@"wav"];
    			
    			AVAudioPlayer * myMusic33=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic3 = myMusic33;
    			myMusic3.delegate = self; 
    			[myMusic3 play];}
    		else if (label3.text == @"5"){[pianoButton3 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku A" ofType:@"wav"];
    			AVAudioPlayer * myMusic33=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic3 = myMusic33;
    			myMusic3.delegate = self; 
    			[myMusic3 play];}
    	}else {
            [pianoButton3 setHighlighted:NO];
        }
    	if(CGRectContainsPoint(pianoButton4.frame, location1d))
        {   if (label4.text == @"0")
    	{[pianoButton4 setHighlighted:YES];
    		NSString *path = [[NSBundle mainBundle] pathForResource:@"uku A" ofType:@"wav"];
    		AVAudioPlayer * myMusic44=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    		self.myMusic4 = myMusic44;
    		myMusic4.delegate = self; 
    		[myMusic4 play];}
    	else if (label4.text == @"1"){[pianoButton4 setHighlighted:YES];
    		NSString *path = [[NSBundle mainBundle] pathForResource:@"uku A#" ofType:@"wav"];
    		AVAudioPlayer * myMusic44=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    		self.myMusic4 = myMusic44;
    		myMusic4.delegate = self;
    		[myMusic4 play];}
    		if (label4.text == @"2")
    		{[pianoButton4 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku B" ofType:@"wav"];
    			AVAudioPlayer * myMusic44=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic4 = myMusic44;
    			myMusic4.delegate = self;
    			[myMusic4 play];}
    		else if (label4.text == @"3"){[pianoButton4 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku 2C" ofType:@"wav"];
    			AVAudioPlayer * myMusic44=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic4 = myMusic44;
    			myMusic4.delegate = self;
    			[myMusic4 play];}
    		if (label4.text == @"4")
    		{[pianoButton4 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku 2C#" ofType:@"wav"];
    			AVAudioPlayer * myMusic44=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic4 = myMusic44;
    			myMusic4.delegate = self;
    			[myMusic4 play];}
    		else if (label4.text == @"5"){[pianoButton4 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku 2D" ofType:@"wav"];
    			AVAudioPlayer * myMusic44=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic4 = myMusic44;
    			myMusic4.delegate = self;
    			[myMusic4 play];}
    	}else {
            [pianoButton4 setHighlighted:NO];
        }
    }
    	
    	
    	
    
    Thanks,
    Nick
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    From iphonedevsdk:

    P.S. Your conditional-logic section seems to contain a lot of replicated code. You might want to consider consolidating / refactoring in a way that, at least, lends itself to being easier to understand and maintain.
     
  3. thread starter macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #3
    Thanks for the reply. I'll definitely refine the conditional statements once I get my other touches working properly. I've seen that post on iphonedevsdk and tried the codes they suggested but with no luck. oh well, search continues.

    Nick
     
  4. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Have you set multipleTouchEnabled?

    Aren't UIGestureRecognizers supposed to make this simpler?
     
  5. thread starter macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #5
    In my viewDidLoad I have
    Code:
            [pianoButton1b setMultipleTouchEnabled:YES];
    	[pianoButton1 setMultipleTouchEnabled: YES ];
    	[pianoButton2 setMultipleTouchEnabled: YES ];
    	[pianoButton3 setMultipleTouchEnabled: YES ];
    	[pianoButton4 setMultipleTouchEnabled: YES ];
    pianoButton1b is the Background and the others are the "strings." Each one is a UIImageView. I'm thinking I must assign an integer for each touch.

    Thanks,
    Nick
     
  6. thread starter macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #6
    I'm still having a little trouble with multi-touch. With "ActiveTouches", I can satisfy my need for multiple touches to fire different actions in touchesBegan. I believe that the View is still only registering 1 touch. Each new touch replaces the first touch. This would be fine, except in touchesMoved if I have 2 fingers on the screen the "touch" ping-pongs causing the 2 actions to fire alternately at a rapid pace. What am I missing to make this a successful multitouch code?

    Code:
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    	
    	if (ActiveTouches == nil)
            ActiveTouches = [[NSMutableArray alloc] init];
    	for (UITouch *touch in touches) {{
            if (![ActiveTouches containsObject:touch])
                [ActiveTouches addObject:touch];
        }
    	CGPoint location1 = [touch locationInView:touch.view];
    
    
        if(CGRectContainsPoint(pianoButton1.frame, location1)) 
        {  if(!pianoButton1.isHighlighted){
    		if (label1.text == @"0")
    		{[pianoButton1 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"ukuG" ofType:@"wav"];
    			if(myMusic)myMusic.volume -= .99;
    			AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic = myMusic11;
    			myMusic.delegate = self;
    			[myMusic play];}
    		else if (label1.text == @"1"){[pianoButton1 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"ukuG#" ofType:@"wav"];
    			if(myMusic)myMusic.volume -= .99;
    			AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic = myMusic11;
    			myMusic.delegate = self;
    			[myMusic play];}
    		else if (label1.text == @"2")
    		{[pianoButton1 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"ukuA" ofType:@"wav"];
    			if(myMusic)myMusic.volume -= .99;
    			AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic = myMusic11;
    			myMusic.delegate = self;
    			[myMusic play];}
    		else if (label1.text == @"3"){[pianoButton1 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"ukuA#" ofType:@"wav"];
    			if(myMusic)myMusic.volume -= .99;
    			AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			
    			self.myMusic = myMusic11;
    			myMusic.delegate = self;
    			[myMusic play];}
    		else if (label1.text == @"4")
    		{[pianoButton1 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"ukuB" ofType:@"wav"];
    			if(myMusic)myMusic.volume -= .99;
    			AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic = myMusic11;
    			myMusic.delegate = self;
    			[myMusic play];}
    		else if (label1.text == @"5"){[pianoButton1 setHighlighted:YES];
    			NSString *path = [[NSBundle mainBundle] pathForResource:@"uku2C" ofType:@"wav"];
    			if(myMusic)myMusic.volume -= .99;
    			AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    			self.myMusic = myMusic11;
    			myMusic.delegate = self;
    			[myMusic play];}}
    	}else {
            [pianoButton1 setHighlighted:NO];
        }
    }
    Thanks,
    Nick
     

Share This Page