如何在动态 ImageView 上创建动态 Button,同时保持唯一性
我正在使用 PhotoScroller 项目,并希望以编程方式在 imageViews 上创建按钮。 ImageView 是 ScrollerView 的子视图。我的代码片段如下所示:
imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
//imageView.tag = i; // tag our images for later use when we place them in serial form
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
[self addSubview:imageView];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button
btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:@selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[self addSubview:btn]; //Buttons are clickable but shows button on all images**
//[imageView addSubview:btn]; //Buttons are not clickable and are shown on all images
我现在有两个选择。我是否将按钮设置为 ImageView 的子视图或 ScrollView(它是 ImageView 的父视图)。如果我制作 Scrollview 的按钮子视图,例如 [self addSubView:btn];它显示在正确的位置并且可单击,但问题是它是在队列中的所有图像上创建的,因为我将其设为父视图(即滚动视图)的子视图。否则,如果我将其设置为子视图的子视图,即 ImageView,它对于每个图像都是唯一的,但仍然显示在所有图像上并且不可单击:/
any1 可以指导我如何使其可单击并保持动态按钮和图像是唯一的,因此我在队列中每个图像的不同位置都有不同的按钮。
提前致谢。
问候,
瓦希卜
I am using PhotoScroller project and want to create buttons over imageViews programmatically. ImageView is a subView of ScrollerView. My code snippet is shown below:
imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
//imageView.tag = i; // tag our images for later use when we place them in serial form
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
[self addSubview:imageView];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button
btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:@selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[self addSubview:btn]; //Buttons are clickable but shows button on all images**
//[imageView addSubview:btn]; //Buttons are not clickable and are shown on all images
I have two choices rite now. Whether i make my button a subview of ImageView or ScrollView which is parent of ImageView. If i make button subview of Scrollview like [self addSubView:btn]; it displays at right position and is clickable but problem is that it is created on all images in the queue because i am making it a subview of parent view i.e scrollview. Else if i make it subview of child view i.e ImageView which is unique for every image, but still its shown on all images and its not clickable :/
Can any1 guide me on how to make it clickable and keeping the linkage between dynamic button and the image unique so that i have different buttons at various positions on each image in the queue.
Thanks in advance.
Regards,
wahib
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须创建一个额外的 UIView 来包含 UIImageView 和 UIButton
注意内存泄漏。您的代码中有很多未使用的保留。
You have to create an extra UIView for containing both UIImageView and UIButton
Caution with memory leak. You have lot of unused retain in your code.