Iphone 如何通过调用imageView上的touchEvent来获取UIimageView中的图像像素?

Iphone 如何通过调用imageView上的touchEvent来获取UIimageView中的图像像素?,iphone,objective-c,uiimageview,touch-event,getpixel,Iphone,Objective C,Uiimageview,Touch Event,Getpixel,我想在imageView上调用touchEvent。在触摸imageView时,我想获得图像的像素。我正在使用此选项:- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; [sampleImage setBackgroundColor:[UIColor redColor]]; //CGPoint location =

我想在imageView上调用touchEvent。在触摸imageView时,我想获得图像的像素。我正在使用此选项:-

- (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]];   
}
有可能吗?如果有,请帮忙。
提前感谢。

您可以使用
UITapgestureRecognitizer
并将
目标设置为您的
图像视图


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

在viewDidLoad中,可以实现以下功能:

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    tap.numberOfTapsRequired = 1;
    [YourImageView addGestureRecognizer:tap];
    [tap release];
- (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];

        }
    }
在代码中,您可以将代理和声音放在如下位置:

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

编辑:


要在触摸屏上使用上述选项,请执行以下操作:

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    tap.numberOfTapsRequired = 1;
    [YourImageView addGestureRecognizer:tap];
    [tap release];
- (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];

        }
    }

好了:)

请参见
userInteractionEnabled
property我可以使用这个方法吗?(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event???当然可以,我将编辑我的答案。BarryK88:-你能告诉我为什么我的这个问题被关闭了吗?因为我是iphone新手,请帮助我??你应该阅读stackoverflow的FAQ->我可以使用这个方法吗?(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件????