2

I'm new to iPhone programing. I'm trying to do a simple window with a cat doing two sounds. When you click at the cat icon it should do "miaau", and when wou drag over window (stroke) it should make "mrrrr". It works, but always when I try to make cat mrrrrr function TouchesBegan fires and cat also do "miaaau".

What to do to make the interface recognize I want only stroke cat, not to touch it for doing the first option, "miaau"?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rafal
  • 55
  • 6

3 Answers3

7

I suggest to add a NSTimer to touchesBegan method with small time interval (say 0.1 sec):

BOOL tap_event = NO; //ivar declared in header

-(void) touchesBegan:... {
    tap_event = YES;
    [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(checkTap:) userInfo: nil repeats: NO];
} 

-(void) checkTap:(NSTimer*) t {
     if( tap_event ) //miauu here
     tap_event = NO;
}

-(void) touchesMoved:... {
    tap_event = NO;
     //mrrrr here
}

Or as an option check docs for UIGestureRecognizers

Max
  • 16,679
  • 4
  • 44
  • 57
  • ok, i will try with BOOL's and let You know it works for me. many thanks. – Rafal Mar 08 '11 at 07:08
  • You could also do a [self performSelector:@selector(checkTap) withObject:nil afterDelay:0.1] if you don't want to have to worry about a timer. – bornbnid Aug 30 '11 at 15:00
1

This may help you

When does a touchesBegan become a touchesMoved?

touchesBegin & touchesMove Xcode Obj C Question

Community
  • 1
  • 1
visakh7
  • 26,380
  • 8
  • 55
  • 69
0

Max' solution is correct. It will definitely work. I am having an alternative, see this approach.

Make your player object in an .h file then allocate it in viewDidLoad and release in dealloc.

-(void) touchesBegan:... {

    //Play the miauu sound.
}

-(void)  touchesMoved:... {
[self.yourPlayer stop];

    //Play mrrrr.
}

Edit

.h file,

AVAudioPlayer *avPlayer1;
AVAudioPlayer *avPlayer2;

.m file

 -(void) touchesBegan:... {
     NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"wav"];
     avPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
     [avPlayer1 play];
 }

-(void) touchesMoved:... {
    [avPlayer1 stop];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"wav"];
    avPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    [avPlayer2 play];
}

-(void)dealloc
{
    [avPlayer1 release];
    [avPlayer2 release];
    [super dealloc];
}
Community
  • 1
  • 1
Ishu
  • 12,797
  • 5
  • 35
  • 51
  • Max solution works. Cat makes miauu when click and mrrrr when stroke but i have problem with: -(void) checkKlik:(NSTimer*) t{ if(klik){ if ([touch view] == kot){ NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"wav"]; AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; [theAudio play]; } klik = NO; } } it shows me that touch is not declared. i need to make IF with touch view kot because i want to only click on "KOT" View. – Rafal Mar 08 '11 at 07:58
  • Because you make your function and touch is the part of touchesBegan and touchesMoved method.ok try my answer it may helps you. – Ishu Mar 08 '11 at 08:26
  • Ishu, when i make Your answer how to show in touchesMoved that miauu cat is playing and stops it? – Rafal Mar 08 '11 at 08:43
  • dear you need to stop your plyer first then restart with mrrr sound. no need to know which file is playing. – Ishu Mar 08 '11 at 08:55
  • but when i try to stop playing in touchesmoved before play another sound it shows me that theAudio is not initialized. – Rafal Mar 08 '11 at 09:20
  • could You tell me please how to pase code in grey colour here? i will paste my code and You could then help me? ok? im begiiner and dont know what is wrong with my simple aplication :( – Rafal Mar 08 '11 at 09:25
  • declare theAudio in .h file.then you can access tha esame instance of that then you can stop it and again make another object with other object of Avplayer you can do it. – Ishu Mar 08 '11 at 10:25
  • http://www.iphonedevsdk.com/forum/iphone-sdk-development/74614-touchesbegan-dont-work-other-void.html , i put some part of my code here. how to declare theaudio in .h? – Rafal Mar 08 '11 at 11:00
  • Ishu, a connot vote that You are genius :) but , thank You very much. It works. – Rafal Mar 08 '11 at 11:48
  • no prob brother but you can accept the answer.and if you really wants vote then you can vote when you have sufficient rep.acceptance also makes your rep. – Ishu Mar 08 '11 at 11:50