【问题标题】:Drag items from a collection view and drop in other view IOS将项目从集合视图拖放到其他视图 IOS
【发布时间】:2014-02-25 04:12:26
【问题描述】:

我有一个包含一些图像的集合视图。我还有另一个 UIView。我希望将集合视图中的图像拖放到 UIView 中,以便在 UIView 上放置一些动作时说颜色发生变化。此外,图像在放置后应保留在其原始位置。

这是我的集合视图代码。

-(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;

}

我尝试平移手势来实现这一点,但没有成功。

- (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];

}

请看屏幕截图。我想将图像从集合视图拖到购物车图像(在 UIView 中)

我应该采取什么方法?有人可以指导我或提供可以提供帮助的代码 sn-ps/links 吗?

【问题讨论】:

标签: ios iphone drag-and-drop uicollectionview uigesturerecognizer


【解决方案1】:

我找到了Markus Gasser 的图书馆。您可以在下面找到它:

Github 上(权威):https://github.com/frenetisch-applaudierend/ios-drag-and-drop 要么 在 Bitbucket 上:https://bitbucket.org/foensi/ios-drag-and-drop

这是一个非常干净的库,附带一个示例应用程序。它展示了如何将视图从一个堆栈移动到另一个堆栈;所有的动画都很好。

警告:与 UICollectionView 一起使用

我已经使用 CollectionViews 进行了尝试:如果您将整个视图注册为 dragSource(演示中显示),我发现您无法再滚动内容。这是因为拖放控制器的手势识别器会捕获所有手势事件,并且不会将其传递给底层的UICollectionView

我问过作者,他是这样说的:

我的意思是实现一种机制来提供不同的手势 用于拖动的识别器,但我的项目中从未真正需要它。 我目前在这种情况下所做的是只定义一个子视图 UICollectionViewCell 作为拖动源。所以如果用户滚动 在集合视图上它仍然可以工作,但是从让我们说 单元格的图标将开始拖放。另一种可能的方式是 是设置一个委托给 UICollectionView panGestureRecognizer 和 实施 -gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer: 在其他平移识别器上失败(尽管我从未使用过这个)。

总而言之,它非常有用——但我仍然需要弄清楚如何让滚动工作。

【讨论】:

  • 是的!在我询问后不久,作者就实现了 longPressGestureRecognizer。这让 CollectionView 滚动。您可以使用最新版本并亲自查看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多