1

我想在 imageView 上调用 touchEvent。在触摸 imageView 时,我想获取 Image 的像素。我正在使用这个:-

- (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]];   
}

有可能吗?如果是,请帮助。提前致谢。

4

2 回答 2

3

您应该在您的图像视图中添加一个手势识别器,并让代理为您处理播放的声音。

在您的 viewDidLoad 中,您实现了以下内容:

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    tap.numberOfTapsRequired = 1;
    [YourImageView addGestureRecognizer:tap];
    [tap release];

在您的代码中,您可以将委托和声音播放如下:

- (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];
}

给你!

编辑:


要将上述选项与 touchesMoved 一起使用,请执行以下操作:

- (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];

        }
    }

你去吧:)

于 2012-04-10T11:13:04.553 回答
0

您可以使用UITapGestureRecognizer并且可以设置target为您的imageview

在此处输入图像描述

于 2012-04-10T11:02:44.350 回答