简体   繁体   English

多个uiimageview touch

[英]multiple uiimageview touch

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. 我在想我将拥有所有图像的数组(NSMutableArray)。 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. 当应用程序加载时,它将把它们绘制到可滚动的UIView ...中,然后该应用程序将知道哪个用户已按下并拖动到主视图中。

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

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!! 感谢您的任何信息!

Matt Facer just posted a question that includes more-or-less the code you're talking about. Matt Facer刚刚发布了一个问题,其中包含或多或少的您正在谈论的代码。

UIImage detecting touch and dragging UIImage检测触摸和拖动

The short answer is that you don't create the UIImages in IB. 简短的答案是您没有在IB中创建UIImage。 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. UITouch事件可用于确定被触摸的视图。

There's probably no need for an array here; 这里可能不需要数组。 an NSMutableSet makes more sense unless there's some special reason for preserving order. 除非出于某些特殊原因保留顺序,否则NSMutableSet更有意义。

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. 要显示和定位图像,请将其放置在UIImageViews中。 The images should be stored in a some kind of data structure eg array, dictionary, database etc. Then you should load them into one or more UIImageViews as needed. 图像应存储在某种数据结构中,例如数组,字典,数据库等。然后应根据需要将它们加载到一个或多个UIImageViews中。

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. 在您的情况下,您将在Interface Builder中连接固定元素,例如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. 然后,当您需要添加图像时,可以初始化UIImageView,并在框架中将其放置在滚动视图中所需的位置。 Then you would set the UIImageView image property to the image you want. 然后,您可以将UIImageView image属性设置为所需的图像。 Then you would add the imageView to the scrollview as a subview. 然后,您可以将imageView作为子视图添加到滚动视图中。

If you need to track the imageView you should create a property for the enclosing view and assign each added imageView in turn. 如果需要跟踪imageView,则应为封闭视图创建一个属性,并依次分配每个添加的imageView。 This way you focus specific methods on the currently selected imageView. 这样,您可以将特定方法集中在当前选择的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. 您确实需要意识到,UIScrollView中的跟踪触摸可能会带来挑战,因为scrollView必须捕获所有触摸以查看是否需要滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM