简体   繁体   English

从集合视图中拖动项目并放入其他视图IOS

[英]Drag items from a collection view and drop in other view IOS

I have a collection view with some images. 我有一些带有一些图像的集合视图。 And I have another UIView. 我有另一个UIView。 I want the image from the collection view to be dragged and drop into UIView such that on dropping on the UIView some action say colour changes. 我希望将集合视图中的图像拖放到UIView中,这样在放弃UIView时,某些操作会说颜色发生变化。 Also the image should remain in its original place after drop. 跌落后,图像应保留在原始位置。

This is my code for the collection view. 这是我的集合视图的代码。

-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell*aCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(3,5,100,100)];
imgView.contentMode = UIViewContentModeScaleAspectFit;
imgView.clipsToBounds = YES;
imgView.image = [UIImage imageNamed: [imagePackArray objectAtIndex:indexPath.row]];
[aCell addSubview:imgView];
 aCell.backgroundColor = [UIColor redColor];
  return aCell;

} }

I tried panning gesture to implement this however was unsuccessful. 我尝试了平移手势来实现这个但是没有成功。

- (IBAction)handlePan:(UIPanGestureRecognizer *)sender {
NSLog(@"panning");
CGPoint translation = [sender translationInView:self.view];
sender.view.center = CGPointMake(sender.view.center.x + translation.x,
                                     sender.view.center.y + translation.y);
[sender setTranslation:CGPointMake(0, 0) inView:self.view];

} }

Please see the screen shot. 请看屏幕截图。 I want to drag the image from the collection view to the cart image (which is in the UIView) 我想将图像从集合视图拖动到购物车图像(在UIView中) 在此输入图像描述

What approach should I take? 我应该采取什么方法? Can someone guide me or provide code snippets/links which can help? 有人可以指导我或提供可以帮助的代码片段/链接吗?

I found a library by Markus Gasser . 我找到了Markus Gasser的图书馆。 You can find it below: 你可以在下面找到它:

On Github (authoritative): https://github.com/frenetisch-applaudierend/ios-drag-and-drop or On Bitbucket: https://bitbucket.org/foensi/ios-drag-and-drop 在Github(权威): https//github.com/frenetisch-applaudierend/ios-drag-and-drop或On Bitbucket: https ://bitbucket.org/foensi/ios-drag-and-drop

It's a very clean library that comes with a sample app. 这是一个非常干净的库,附带一个示例应用程序。 It shows how you can move views from one stack to another; 它显示了如何将视图从一个堆栈移动到另一个堆栈; all nicely animated. 一切都很好动画。

Caveat: Using with UICollectionView 警告:使用UICollectionView

I've tried it out with CollectionViews: if you register the whole view as a dragSource (which the demo shows), I found that you can no longer scroll the content. 我已经用CollectionViews试了一下:如果你将整个视图注册为dragSource (演示显示),我发现你不能再滚动内容了。 This is because the drag-and-drop controller's gesture recogniser captures all gesture events and does not pass it through to the underlying UICollectionView . 这是因为拖放控制器的手势识别器捕获所有手势事件,并且不会将其传递给基础UICollectionView

I asked the author about this, and he had this to say: 我向作者询问了这个问题,他这样说:

I meant to implement a mechanism to provide a different gesture recognizer for dragging, but I never really needed it in my projects. 我的意思是实现一种机制,为拖动提供不同的手势识别器,但我从来没有真正需要它在我的项目中。 What I'm currently doing in this situation is to define only a subview of the UICollectionViewCell as the drag source. 我目前在这种情况下所做的是仅定义UICollectionViewCell的子视图作为拖动源。 So if the user scrolls over the collection view it'll still work, but dragging from let's say the cell's icon will start drag and drop. 因此,如果用户滚动集合视图它仍然可以工作,但是从让我们说拖动单元格的图标将开始拖放。 Another possible way would be to set a delegate to the UICollectionView panGestureRecognizer and implement -gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer: to fail on other pan recognizers (though I never used this). 另一种可能的方法是将委托设置为UICollectionView panGestureRecognizer并实现-gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer:在其他pan识别器上失败(尽管我从未使用过此)。

So all in all very useful — but I've still got to work out how to make scrolling work. 所有这一切都非常有用 - 但我仍然需要弄清楚如何使滚动工作。

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

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