相对于finger,拖动UICollectionViewCell

时间:2015-01-29 16:47:39

标签: ios objective-c uicollectionview uicollectionviewcell

我正在尝试相对于手指拖动UICollectionViewCell。这并不意味着重新排序CollectionView,我只想让单元格可以拖动。当前代码适用于UIView,但不适用于单元格。

以下是我添加到单元格中的内容:

UIPanGestureRecognizer* drag;
    drag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragging:)];

    [cell addGestureRecognizer:drag];

这是调用的方法。该方法肯定已激活,但不会拖动单元格。

-(void)dragging:(UIPanGestureRecognizer *)gesture
{
    NSLog(@"dragging");
    CGPoint panCoord;
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        NSLog(@"Received a pan gesture");
        panCoord = [gesture locationInView:gesture.view];


    }
    CGPoint newCoord = [gesture locationInView:gesture.view];
    float dX = newCoord.x-panCoord.x;
    float dY = newCoord.y-panCoord.y;

    gesture.view.frame = CGRectMake(gesture.view.frame.origin.x+dX, gesture.view.frame.origin.y+dY, gesture.view.frame.size.width, gesture.view.frame.size.height);
}

0 个答案:

没有答案