多个 uiimageview 触摸

发布于 2024-08-17 02:40:53 字数 411 浏览 3 评论 0原文

我只是想计划一个新的应用程序...我想要一系列图像,用户可以在屏幕上拖动这些图像并将其放在他们想要的位置。

我想我将拥有一个包含所有图像的数组(NSMutableArray)。当应用程序加载时,它将把它们绘制到可滚动的 UIView...然后应用程序将知道用户按下了哪些内容并将其拖入主视图。

我可以对其进行编码,以便可以拖动一张图像,但我不确定如何将相同的代码应用于 n 个图像。例如,在我的视图控制器中,我设置了 IBOutlet UIImage *image ,在 IB 中我可以链接和控制......但是如何更改它以应用于任意数量的图像?

我整个早上都在尝试寻找这个,但运气不佳。

我想当我从数组中绘制图像时,我需要对每个图像应用一个类。 我只是想在这里得到一些建议——也许能为我指明正确的方向?

感谢您提供任何信息!

I am just trying to plan a new app... I want to have a series of images which a user can drag around the screen and drop where they want.

I'm thinking that I will have an array (NSMutableArray) of all the images. when the app loads, it will draw these out to a scrollable UIView... then the app will know which a user has pressed and dragged in to the main view.

I can code it so one image can be dragged, but I'm not sure how to apply the same code to n number of images. For example, in my view controller, I set IBOutlet UIImage *image which in IB I can link and control.... but how do you change this to apply to any number of images?

I've tried searching for this all morning but haven't had much luck.

I am thinking I'd need to apply a class to each image as I draw it out of the array.
I'm just after a little advice here really - maybe pointing me in the right direction?

Thanks for any info!!

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

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

发布评论

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

评论(2

甲如呢乙后呢 2024-08-24 02:40:54

首先,您需要在逻辑上将数据(在本例中为图像)与显示它们的视图分开。要显示和定位图像,请将它们放置在 UIImageViews 中。图像应该存储在某种数据结构中,例如数组、字典、数据库等。然后您应该根据需要将它们加载到一个或多个 UIImageView 中。

如果界面元素的数量变化很大,最好以编程方式创建可变元素。在您的情况下,您将在 Interface Builder 中连接固定元素,例如滚动视图。然后,当您需要添加图像时,您可以使用框架将其定位在滚动视图中所需的位置来初始化 UIImageView。然后,您可以将 UIImageView 图像属性设置为您想要的图像。然后,您将 imageView 作为子视图添加到滚动视图中。

如果您需要跟踪 imageView,您应该为封闭视图创建一个属性,并依次分配每个添加的 imageView。这样您就可以将特定方法集中在当前选定的 imageView 上。

您确实需要注意,跟踪 UIScrollView 中的触摸可能会带来挑战,因为滚动视图必须捕获所有触摸以查看是否需要滚动。

First, you need to logically separate your data, in this case images, from the views that display them. To display and position images you put them UIImageViews. The images should be stored in a some kind of data structure e.g. array, dictionary, database etc. Then you should load them into one or more UIImageViews as needed.

If you have a highly variable number of interface elements it is best to create the variable elements programatically. In your case, you would wire up in Interface Builder the fixed elements like the scrollview. Then when you needed to add an image you initialize a UIImageView with a frame positioning it where you want it in the scrollview. Then you would set the UIImageView image property to the image you want. Then you would add the imageView to the scrollview as a subview.

If you need to track the imageView you should create a property for the enclosing view and assign each added imageView in turn. This way you focus specific methods on the currently selected imageView.

You do need to be aware that tracking touches in a UIScrollView can present challenges because the scrollView has to trap all touches to see if it needs to scroll.

像你 2024-08-24 02:40:53

Matt Facer 刚刚发布了一个问题,其中或多或少包含您正在谈论的代码。

UIImage 检测触摸和拖动

简短的答案是,您不会在中创建 UIImage IB。您将以编程方式创建并根据需要将它们添加到主视图中。 UITouch 事件可用于确定被触摸的视图。

这里可能不需要数组; NSMutableSet 更有意义,除非有一些特殊原因需要保留顺序。

Matt Facer just posted a question that includes more-or-less the code you're talking about.

UIImage detecting touch and dragging

The short answer is that you don't create the UIImages in IB. You'll create the programmatically and add them to the main view as you need them. The UITouch event can be used to determine the view that was touched.

There's probably no need for an array here; an NSMutableSet makes more sense unless there's some special reason for preserving order.

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