问题描述
在我的iPad中,我有一个视图,里面有左图,我有一个带10个imageViews的scrollview;所以我应该把这些图像从scrollview拖放到我的大子视图;我该怎么做?
In my iPad I have a view and inside it, on the left, I have a scrollview with 10 imageViews; so I should drag and drop these images from scrollview to my big subview; how can I do it?
推荐答案
我做了一些像你在描述的东西。我记得拉出来,我创建了一个新的 UIGestureRecgonizer
,我被称为 UIDownwardDragGestureRecognizer
。我想我再次通过滚动浏览手势识别器来要求他们等待 UIDownwardGestureRecognizer
失败如下:
I did something like what you are describing once. I remember to pull it off I created a new UIGestureRecgonizer
that I dubbed UIDownwardDragGestureRecognizer
. I think I then iterated through the scrollviews gesture recognizers to require them to wait for the UIDownwardGestureRecognizer
to fail like:
UIDownwardDragGestureRecognizer *downwardGesture = [UIDownwardGestureRecognizer alloc] initWithTarget:self action:@selector(downwardGestureChanged:)];
[myScrollview addGestureRecognizer:downwardGesture];
for (UIGestureRecognizer *gestureRecognizer in myScrollview.gestureRecognizers)
{
[gestureRecognizer requireGestureRecognizerToFail:myDownwardGesture];
}
一旦你有了这个设置,你应该能够做一些如
Once you have this setup, you should be able to do something like:
- (void) downwardGestureChanged:(UIDownwardDragGestureRecognizer*)gesture
{
CGPoint point = [gesture locationInView:myScrollView];
if (gesture.state == UIGestureRecognizerStateBegan)
{
UIView *draggedView = [myScrollView hitTest:point withEvent:nil];
if ([draggedView isTypeOfClass:[UIImageView class]])
{
self.imageBeingDragged = (UIImageView*)draggedView;
}
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
self.imageBeingDragged.center = point;
}
else if (gesture.state == UIGestureRecognizerStateEnded ||
gesture.state == UIGestureRecognizerStateCancelled ||
gesture.state == UIGestureRecognizerStateFailed)
{
// Determine if dragged view is in an OK drop zone
// If so, then do the drop action, if not, return it to original location
self.imageBeingDragged = nil;
}
}
在UIGestureRecognizerStateBegan中,一旦找到UIImageView,您可以要从superview(scrollview)中删除它,并将其作为子视图添加到其他容器中。如果这样做,您将需要将点转换为新的坐标空间。如果您想要将原始图像留在滚动视图中,请制作一份副本,并将其添加到外部容器。
In the UIGestureRecognizerStateBegan, once you find a UIImageView you may want to remove it from the superview (scrollview) and add it as a subview to some other container. You will need to translate the point into the new coordinate space if you do this. If you want the original image to stay in the scrollview, then make a copy of it and add that to an outside container.
要尝试上述示例并查看它工作时,您可能需要关闭scrollview上的剪辑,因为上面给出的示例是从scrollview内部拖动UIImageView(通常您会将其添加到某些包含的视图)。
To try out the above example and see it working, you may need to turn off clipping on the scrollview since the example I gave above is dragging the UIImageView from inside the scrollview (though normally you'd add it to some containing view).
这篇关于IOS:将scrollview中的imageview拖放到视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!