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

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

问题描述:

我想打电话给在ImageView的的imageView.On触摸的的TouchEvent,我希望得到一个Image.I的像素现在用这样的: -如何通过在imageView上调用touchEvent来获取UIimageView中图像的像素?

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

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

+1

看到'userInteractionEnabled'财产 – beryllium 2012-04-10 11:03:15

您应该添加一个手势识别您的ImageView,让委托处理声音播放给你。

在您的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]; 

     } 
    } 

你去那里:)

+0

我可以使用这种方法 - (void)touchesBegan:(NSSet *)触及事件:(UIEvent *)事件???? – 2012-04-10 11:51:52

+0

是的,我会编辑我的答案。 – BarryK88 2012-04-10 12:23:30

+0

:-Thanks很多:) – 2012-04-10 12:30:37

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

enter image description here

+0

我可以用这个方法 - (void)touchesBegan:(NSSet *)触及事件:(UIEvent *)事件???? – 2012-04-10 11:52:20