如何将 uiimage 从滚动视图拖动到 iphone sdk 中的另一个 uiimageview
在我的应用程序中,我有一个包含多个图像的滚动视图。
在滚动视图之外,我有一个 uiimageview。
我想从 ScrollView 中拖动任何图像并将其放在滚动视图之外的 uiimageview 上。
是否可以?
感谢帮助和建议
提前致谢。
In My Application,i am having one scrollVIew containing multiple images.
and out of the scrollview i have one uiimageview.
i want to Drag any image from ScrollView and drop it on uiimageview which is out of the scrollview.
is it possible?
help And suggestions are appreciated
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
gamozzii的回复与您需要做的很接近,但有一个问题。 UIScrollView 会吃掉触摸,因此点击图像不会产生任何效果。
因此,您必须对 UIScrollView 进行子类化,
我编写了一个小型功能应用程序来说明将图像从滚动视图拖动到图像视图中。您可以在此处下载该项目。
gamozzii's reply is close to what you need to do, but there's one problem. An UIScrollView will eat touches, so tapping on an image will have no effect.
As a result you will have to subclass the 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.
是的,这应该是可能的,您需要直接实现触摸/拖动事件。
查看UIResponder 中的touchesBegan、touchesMoved 等委托方法。
一种方法是子类化 imageview 并在其中实现 TouchBegan、touchesMoved,并使用此 ImageView 子类在滚动视图中显示图像。
在touchesBegan上创建一个新的图像视图并将其添加到外部视图并将其图像设置为与滚动视图中的图像相同。您需要将其直接覆盖在滚动视图中的源图像上,因此调整其框架原点以相对于外部视图,您将需要使用滚动视图原点以及内容视图大小和内容内源图像视图的偏移量view 以便重新计算外部视图中的新原点。
然后,在移动的触摸上,只需根据移动的触摸的坐标重新调整该图像的框架,以便图像跟随触摸。
对目标图像视图的框架进行边界检查 - 一旦用户将其拖动到此边界,使该目标图像视图图像与被拖动视图中的图像相同,并从包含视图中删除拖动的图像并释放它。
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.
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.
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. 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.
如果您仍然对另一个解决方案感兴趣(当然还有其他用户):
我之前确实实现了该行为,就像 gamozzii 推荐的那样。
我在
UIScrollView
上设置了canCancelContentTouches = NO
以确保子视图自行处理触摸。如果触摸了子视图(在您的情况下是图像),我将视图从滚动视图移到超级视图上,并开始跟踪它的拖动。 (您必须在新的超级视图中计算正确的坐标,以便它保持在原位)。拖动完成后,我检查是否到达目标区域,否则我将其移回到滚动视图中。子视图通过(
touchesBegan:/Moved:/Ended:/Cancelled:
)自行处理拖动。如果这还不够详细,这是我的示例代码:Github:JDDroppableView
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.
I set
canCancelContentTouches = NO
on theUIScrollView
to make sure the subviews handle there touches on their own. 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:
).If that's not detailed enough, here's my example code: Github: JDDroppableView