如何从 UIScrollView 中的多个 imageView 中识别特定 imageView 上的触摸?

发布于 2024-11-11 18:41:39 字数 1077 浏览 4 评论 0原文

我在 UIScrollView 中有多个 ImageView,并且我想识别我触摸的特定 imageView。这样我就可以采取一些行动。 (例如,显示带有图像名称的警报)。

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

如果有人能帮忙举一个简单的例子,那就太好了。 :-)

我的代码是这样的

- (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 ?????????? :-)

}

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).

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. :-)

my code is like this

- (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 ?????????? :-)

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

习惯成性 2024-11-18 18:41:39

我建议您查看 的文档和示例UIGestureRecognizer。还有一个更具解释性的事件处理指南< /a> .

这将引导您将手势识别器添加到特定视图(例如您的 imageView),这些视图可以根据您的喜好处理滑动、点击和多点触控手势。

I recommend you look through the documentation and examples for UIGestureRecognizer. There is also a more explanatory Event Handling Guide .

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.

葬花如无物 2024-11-18 18:41:39

您可以使用 UIButton 而不是图像视图。为每个按钮分配标签,然后使用 switch(...) 识别它们。

You can use UIButton instead of image view. Assign tags for each button and then identify them using switch(...).

埖埖迣鎅 2024-11-18 18:41:39

将唯一的标签值与每个 UIImageView 实例相关联:

在 viewDidLoad 中或初始化图像的任何位置:

myImageView1.tag = 1;
myImageView2.tag = 2;
myImageView3.tag = 3;

然后尝试手势或类似的操作:

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event       
{
UITouch *touch = [[event allTouches] anyObject];
int t = [touch view].tag;
UIImageView *imageView = (UIImageView *) [self.view  viewWithTag:t];
//your logic here since you have recognized the imageview on which user touched
}

以前没有尝试过类似的操作,但它可能应该有效。

Associate a unique tag value with each UIImageView instance:

In viewDidLoad or wherever your images are initialised:

myImageView1.tag = 1;
myImageView2.tag = 2;
myImageView3.tag = 3;

then try a gesture or something like:

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event       
{
UITouch *touch = [[event allTouches] anyObject];
int t = [touch view].tag;
UIImageView *imageView = (UIImageView *) [self.view  viewWithTag:t];
//your logic here since you have recognized the imageview on which user touched
}

haven't tried something like this before but it should probably work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文