views:

274

answers:

3

heey...

I have two buttons (Button 1 and button 2) and if i press button 1, note1 starts and if i press button 2, note 2 starts. So what I try to do is: press button one (note1 starts) and SLIDE to button2 and than should note2 start. Like you slide without lifting your finger over a piano keyboard and all notes from c to h sound.

I did this with UIImageViews and i did it also with UIButtons. If i press the c1pianoview it sounds a "c". if i press the d1pianoview it sounds a "d". But if i slide without lifting my finger from c1pianoview to d1pianoview it only sounds a "c". What did I wrong? Can I do it also with UIButtons? Touch down works but sliding does'nt work! Can somebody help me, please? Here my code!

-(void)touchesBeganNSSet *)touches withEventUIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];

if(CGRectContainsPoint(c1pianoview.frame, location))

{ NSString *path = [[NSBundle mainBundle]
pathForResource: @"c1piano" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
[theAudio play];
}


if(CGRectContainsPoint(d1pianoview.frame, location))

{ NSString *path = [[NSBundle mainBundle]
pathForResource: @"d1piano" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPathath] error:NULL];
[theAudio play];
}

}

Thx for help!

A: 

Don't you need the:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
willcodejavaforfood
ok i did it with touchesMoved.. but now every time i slide in my ImageView the note sounds. But i want that the note only once sounds. what did i wrong? thx for help!
@franhu - Dimitris beat me to it
willcodejavaforfood
A: 

You can probably achieve this easily by using the "Touch Down" and "Touch Drag Inside" events of the UIButton.

If you want to use the -touchesMoved: approach, you can use a variable to keep track of the last button that triggered a sound and only play a sound if the current button is not the one that last played a sound.

In more detail:

Declare a UIView *lastButton; in your header and then:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];

if(CGRectContainsPoint(c1pianoview.frame, location) && lastButton != c1pianoview) {
    //play c1
    lastButton = c1pianoview;
}
else if(CGRectContainsPoint(d1pianoview.frame, location) && lastButton != d1pianoview) {
    //play d1
    lastButton = d1pianoview;
}
}

- (void)touchedEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    lastButton = nil;
}

- (void)touchedCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesEnded:touches withEvent:event];
}
Dimitris
Ok it doesnt work with TouchDragInside... can you help me with touchesMoved.. wich cariable? thx
I have edited my post. Let me know if that helps :)
Dimitris
Sorry but it still doesnt work. If i move inside the c1pianoview or the d1pianoview it the note sounds by every little movement. So if i slide from c1pianoview to d1pianoview, there sound about 10 c-notes and 10 d-notes. =) but i only want hear one c-note and one d-note! Do know why i doesnt work? thx for helping!greez
YEEEEESS maaaan... thanks a lot.. it works.. i did it wrong.. yeah you rock.. great thank you!
Ok sliding works. but there is an onther small problem. If i click on c1-button five times it shoud sound five times a "C" so i made it with the touchesBegan method. This works but know the other problem is that if i click on c1-button and then slide inside the note sounds two times. I dont know if you understand my problem but thanks if you could help! greez
I added a few more methods to my answer. They reset the `lastButton` variable whenever the finger is lifted. Have a look.
Dimitris
Sorry i got still two notes if i click and drag. But you said its possible to do it with buttons? I think it's easier with buttons isn't it? can you give me start help. sorry but i'm a noob. xD thanx a lot!
A: 

sorry i got another question.. now i have two UIImageVIews on each other... a black piano key over two white piano keys.. so if i press on the black key it also sound the note from the white key under it? can you help me?

thx