I want to call an touchEvent on an imageView.On touch of a imageView,i want to get a pixel of an Image.I am using this:-
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
[sampleImage setBackgroundColor:[UIColor redColor]];
//CGPoint location = [touch locationInView:self.view];
if ([touch view] == sampleImage)
{
url1 = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Na.wav", [[NSBundle mainBundle] resourcePath]]];
[sampleImage setBackgroundColor:[UIColor colorWithRed:10.0/255.0 green:10.0/255.0 blue:10.0/255.0 alpha:1.0]];
}
NSError *error;
audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:&error];
audio.numberOfLoops = 0;
[audio play];
[sampleImage setBackgroundColor:[UIColor redColor]];
}
Is it possible?if Yes then Please Help. Thanks In Advance.
You should add a gesture recognizer to your imageview and let the delegate handle the sound playing for you.
In your viewDidLoad you implement the following:
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tap.numberOfTapsRequired = 1;
[YourImageView addGestureRecognizer:tap];
[tap release];
In your code you can put the delegate and the sound playing like:
- (void)tapGesture:(UIGestureRecognizer*)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:@"YourSound" ofType:@"mp3"];
AVAudioPlayer * theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
[theAudio release];
}
There you go!
EDIT:
To use above option with touchesMoved implement the following:
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *) event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if ([touch view] == YourImageView) {
//play the sound
NSString *path = [[NSBundle mainBundle] pathForResource:@"YourSound" ofType:@"mp3"];
AVAudioPlayer * theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
[theAudio release];
}
}
There you go :)