1

I want to drag a UICollectionViewCell on another UIView or another element. I've created a subclass of UICollectionViewCell.

This is my cell class, subclass of 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

I can drag and drop any UICollectionViewCell in UICollectionView. But I can't drag a cell on another element on the screen. For example another UICollectionView or UIView.

If I set clipToBounds property to "false" then I can drag a cell on anywhere but it doesn't hide overflow content, like scrolled cells.

in this picture, clipToBounds=false:

enter image description here

onivi
  • 1,348
  • 3
  • 17
  • 25

1 Answers1

1

I don't know if is is possible or not but why you don't just put a UIView over UICollectionViewCell and drag this UIView? When user tries to drag a cell, just hide the cell and add a UIView that acts like the cell.

amone
  • 3,712
  • 10
  • 36
  • 53