简体   繁体   English

如何将uiimage从scrollview拖到iphone sdk中的另一个uiimageview

[英]how to drag an uiimage from scrollview to another uiimageview in iphone sdk

In My Application,i am having one scrollVIew containing multiple images. 在我的应用程序中,我有一个包含多个图像的scrollVIew。

and out of the scrollview i have one uiimageview. 从滚动视图中我有一个uiimageview。

i want to Drag any image from ScrollView and drop it on uiimageview which is out of the scrollview. 我想从ScrollView拖动任何图像并将其放在滚动视图之外的uiimageview上。

is it possible? 可能吗?

help And suggestions are appreciated 帮助和建议表示赞赏

Thanks in advance. 提前致谢。

gamozzii's reply is close to what you need to do, but there's one problem. gamozzii的回复接近你需要做的,但有一个问题。 An UIScrollView will eat touches, so tapping on an image will have no effect. UIScrollView会触摸,因此点击图像将无效。 As a result you will have to subclass the UIScrollView 因此,您必须继承UIScrollView的子类

I have written a small functional app to illustrate dragging the image from a scroll view into an image view. 我编写了一个小功能应用程序,用于说明将图像从滚动视图拖动到图像视图中。 You can download the project here. 您可以在此处下载该项目

Yes this should be possible, you will need to implement the touch/drag events directly. 是的,这应该是可能的,您需要直接实现触摸/拖动事件。

Check out the touchesBegan, touchesMoved etc. delegate methods in UIResponder. 查看UIResponder中的touchesBegan,touchesMoved等委托方法。

One approach would be to subclass imageview and implement touchesBegan, touchesMoved in it, and use this imageview subclass to display your images in the scroll view. 一种方法是将imageview子类化并在其中实现touchesBegan,touchesMoved,并使用此imageview子类在滚动视图中显示图像。

On the touchesBegan create a new image view and add it to the outer view and set its image to be the same as the one in the scroll view. 在touchesBegan上创建一个新的图像视图并将其添加到外部视图并将其图像设置为与滚动视图中的图像相同。 You need to overlay it directly over your source image in the scroll view so adjust its frame origin to be relative to the outer view you will need to use the scrollview origin and also the content view size and offset of the source image view inside the content view in order to recalculate the new origin in the outer view. 您需要在滚动视图中将其直接覆盖在源图像上,因此将其框架原点调整为相对于外部视图,您需要使用滚动视图原点以及内容视图大小和内容中的源图像视图的偏移量查看以重新计算外部视图中的新原点。

Then on the touches moved, simply readjust the frame of this image in accordance with the coordinates from the touches moved so that the image follows the touch. 然后在移动的触摸上,根据移动的触摸的坐标简单地重新调整该图像的帧,使得图像跟随触摸。

Do a boundary check against the frame of your target imageview - once the user drags it into this boundary, make that target imageviews image the same as the image in the view being dragged and remove the dragged image from the containing view and release it. 对目标图像视图的框架进行边界检查 - 一旦用户将其拖入此边界,使目标图像视图图像与拖动视图中的图像相同,并从包含视图中移除拖动的图像并将其释放。

In case you're still interested in another solution (and for other users of course): 如果您仍然对另一个解决方案(当然还有其他用户)感兴趣:

I did implement that behaviour before just like gamozzii recommended. 我之前就像gamozzii推荐的那样实现了这种行为。

I set canCancelContentTouches = NO on the UIScrollView to make sure the subviews handle there touches on their own. 我在UIScrollView上设置了canCancelContentTouches = NO ,以确保子视图处理自己的触摸。 If a subview (in your case an image) was touched, i moved the view out of the scrollview onto the superview and started tracking it's dragging. 如果触摸了子视图(在您的情况下是图像),我将视图从滚动视图移动到超视图上并开始跟踪它的拖动。 (You have to calculate the correct coordinates within the new superview, so it stays in place). (您必须在新的超视图中计算正确的坐标,因此它保持不变)。 After dragging finishes, i checked if the target area was reached, otherwise I moved it back into the scrollview. 拖动完成后,我检查是否到达目标区域,否则我将其移回滚动视图。

The subviews are handling the dragging on there own via ( touchesBegan:/Moved:/Ended:/Cancelled: ). 子视图正在处理那里的拖动( touchesBegan:/Moved:/Ended:/Cancelled:

If that's not detailed enough, here's my example code: Github: JDDroppableView 如果这还不够详细,这是我的示例代码: Github:JDDroppableView

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

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