IScrollView中的多个imageView识别特定imag

IScrollView中的多个imageView识别特定imag

本文介绍了如何从UIScrollView中的多个imageView识别特定imageView上的触摸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UIScrollView中有多个ImageView,并且我想确定所触摸的特定imageView .这样我就可以做一些动作.(例如,显示带有图片名称的Alert).

I have multiple ImageViews in a UIScrollView and I want to identify the specific imageView on which I touched. So that I could do some actions. (for example, showing Alert with the image name).

我看到过一些类似的帖子,但是他们没有适当的答案.对我来说,有些答案很复杂,因为我对iPhone编程还很陌生.

I have seen some similar posts, but they did not have proper answer. And some answers were complex to me as I am pretty new to iPhone programming.

如果有人帮助您举一个简单的例子,那就太好了.:-)

It would be great, If anybody helps with a simple example. :-)

我的代码就是这样

- (void)viewDidLoad {


imgScrollView.userInteractionEnabled =YES;

// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= 5; i++)
{
    NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", i];
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    // setup each frame to a default height and width
    CGRect rect = imageView.frame;
    rect.size.height = kHeight;
    rect.size.width = kWidth;
    imageView.frame = rect;
    imageView.tag = i;

    [imgScrollView addSubview:imageView];

    [imageView release];
}


[self layoutImages];

[super viewDidLoad];
}

     -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// WHAT CODE SHOULD I GIVE HERE ?????????? :-)

}

推荐答案

我建议您仔细阅读 UIGestureRecognizer .事件处理指南.

这将引导您完成将手势识别器添加到特定视图(例如您的imageView)的过程,这些视图可以处理您喜欢的滑动,点击和多点触摸手势.

This will walk you through adding Gesture Recognizers to specific views (e.g. your imageView) that can handle swipes, taps and multitouch gestures however you like.

这篇关于如何从UIScrollView中的多个imageView识别特定imageView上的触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 21:59