ios - 将 UICollectionViewCell 拖动到另一个元素上

标签 ios drag-and-drop uicollectionview uicollectionviewcell cliptobounds

我想将一个 UICollectionViewCell 拖到另一个 UIView 或另一个元素上。 我已经创建了 UICollectionViewCell 的子类。

这是我的单元类,UICollectionViewCell 的子类:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.dragObject=self;
    self.homePosition = CGPointMake(self.frame.origin.x,
                                        self.frame.origin.y);
    [self.superview.superview bringSubviewToFront:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [[touches anyObject] locationInView:self.superview];
    CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
                                           touchPoint.y - touchOffset.y,
                                           self.frame.size.width,
                                           self.frame.size.height);
    self.frame = newDragObjectFrame;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{


            CGRect newFrame =  CGRectMake(self.homePosition.x, self.homePosition.y,
                                          self.frame.size.width,
                                          self.frame.size.height);


            [UIView animateWithDuration:0.2
                             animations:^{
                                 self.frame = newFrame;
                             }];

}

enter image description here

我可以在 UICollectionView 中拖放任何 UICollectionViewCell。但我无法将单元格拖到屏幕上的另一个元素上。例如另一个 UICollectionView 或 UIView。

如果我将 clipToBounds 属性设置为“false”,那么我可以将单元格拖到任何地方,但它不会 overflow hidden 的内容,例如滚动的单元格。

在这张图片中,clipToBounds=false:

enter image description here

最佳答案

我不知道是否可行,但为什么不直接将 UIView 放在 UICollectionViewCell 上并拖动此 UIView? 当用户试图拖动一个单元格时,只需隐藏该单元格并添加一个像单元格一样的 UIView。

关于ios - 将 UICollectionViewCell 拖动到另一个元素上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19208404/

相关文章:

iphone - 隐藏导航栏点击不工作

iphone - 如何在 iOS 中递归地获取我的包中的所有资源路径?

javascript - jQuery 在拖动时使用右键单击旋转 div

ios - 更改 UICollectionView 中特定单元格的图像

ios - 将 UIImagepicker 中的图像插入到 UICollectionViewCell Swift 中

ios - 快速选项卡栏尝试在某些页面上显示登录 View Controller

ios - 在 Swift 3 中正确格式化 JSON 对象

javascript - 如何使用 JavaScript 暂时禁用文本选择?

javascript - 使用 react-beautiful-dnd 嵌套拖放

ios - UICollectionView 理解可见单元格日志