UICollectionView与手势识别器

时间:2014-01-15 11:34:24

标签: ios objective-c uicollectionview

我有两个UICollectionView,我想提供将对象从一个UICollectionView移动到另一个{。\ n}。

所以,当它longGesture然后panGesture时,我想创建一个UIImageView这个被点击的单元格UIImageView并将其移动到{{1}时的位置结束。

如果此图像框位于第二个panGesture帧中。

从视图中删除此图像,并将此对象添加到第二个UICollectionView的数组中。

我怎么能提供这个?

1 个答案:

答案 0 :(得分:2)

我做到了但是我在Hold Tap

上做了
    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressOnCollectionView:)];
    lpgr.minimumPressDuration = .3; //seconds
    lpgr.numberOfTouchesRequired=1;
    lpgr.delegate = self;
    [self.collectionView addGestureRecognizer:lpgr];


- (void)handleLongPressOnCollectionView:(UILongPressGestureRecognizer *)gestureRecognizer

{
    CGPoint p = [gestureRecognizer locationInView:self.collectionView];
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        cellInitialPoint = [gestureRecognizer locationInView:self.view];

        pAddByHolding = [gestureRecognizer locationInView:self.collectionView];
        ipAddByHolding = [self.collectionView indexPathForItemAtPoint:pAddByHolding];
        NSIndexPath *ip = [self.collectionView indexPathForItemAtPoint:p];
        UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:ip];
        UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:ip];
        UIView *view = (UIView*)cell.contentView;
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage * resultingImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageView *imageViewToAdd = [[UIImageView alloc] initWithImage:resultingImage];
        imageViewToAdd.layer.cornerRadius=5;
        imageViewToAdd.layer.borderColor=COLLECTIONVIEW_BACKGROUND.CGColor;
        imageViewToAdd.layer.borderWidth=0;
        cellAnimationView =  [[UIView alloc] initWithFrame:imageViewToAdd.frame];
        cellAnimationView.tag = 9999999;
        CGRect rect = CGRectMake(attributes.frame.origin.x , attributes.frame.origin.y +        self.collectionView.frame.origin.y , imageViewToAdd.frame.size.width, imageViewToAdd.frame.size.height);
        NSLog(@"%@",NSStringFromCGPoint([attributes frame].origin));

        [cellAnimationView setFrame:rect];
        cellAnimationView.layer.cornerRadius=5;
        cellAnimationView.layer.borderColor=COLLECTIONVIEW_BACKGROUND.CGColor;
        cellAnimationView.layer.borderWidth=0;
        [cellAnimationView addSubview:imageViewToAdd];
        OAAppDelegate *appDelegate =(OAAppDelegate*)[[UIApplication sharedApplication]delegate];
        [appDelegate.window addSubview:cellAnimationView];
    }
    else if(gestureRecognizer.state == UIGestureRecognizerStateChanged)
    {
        CGPoint p2 = [gestureRecognizer locationInView:self.view];
        [cellAnimationView setFrame:CGRectMake(p2.x-cellAnimationView.frame.size.width/2, p2.y - cellAnimationView.frame.size.height/2, cellAnimationView.frame.size.width, cellAnimationView.frame.size.height)];
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        pathAnimation.calculationMode = kCAAnimationPaced;
        pathAnimation.fillMode = kCAFillModeRemoved;
        pathAnimation.removedOnCompletion = YES;
        pathAnimation.duration=0.65;
        pathAnimation.delegate=self;
        OAAppDelegate *appDelegate =(OAAppDelegate*)[[UIApplication sharedApplication]delegate];
        p = [gestureRecognizer locationInView:appDelegate.window];
        if (p.x > 200 && p.y < 250) //Check if its outside frame of UICollectionViewCell
        {
            CGPoint endPoint = CGPointMake(290, 38);

            CGMutablePathRef curvedPath = CGPathCreateMutable();
            NSLog(@"%@",NSStringFromCGPoint([cellAnimationView frame].origin));
            CGPathMoveToPoint(curvedPath, NULL, cellAnimationView.frame.origin.x, cellAnimationView.frame.origin.y);
            //CGPathMoveToPoint(curvedPath, NULL, cellAnimationView.frame.origin.x, cellAnimationView.frame.origin.y);
            CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, cellAnimationView.frame.origin.y, endPoint.x, cellAnimationView.frame.origin.y, endPoint.x, endPoint.y);
            pathAnimation.path = curvedPath;
            CGPathRelease(curvedPath);

            // end ---- apply position animation

            // apply transform animation
            CABasicAnimation *basic=[CABasicAnimation animationWithKeyPath:@"transform"];
            [basic setToValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.05, 0.05, 0.25)]];
            [basic setAutoreverses:NO];
            [basic setDuration:0.65];

            [cellAnimationView.layer addAnimation:pathAnimation forKey:@"curveAnimation"];
            [cellAnimationView.layer addAnimation:basic forKey:@"transform"];
            [cellAnimationView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.65];

        }
        else    // animation view to orignal position
        {
            CGPoint endPoint = cellInitialPoint;

            CGMutablePathRef curvedPath = CGPathCreateMutable();

            CGPathMoveToPoint(curvedPath, NULL,       cellAnimationView.frame.origin.x+cellAnimationView.frame.size.width/2, cellAnimationView.frame.origin.y+cellAnimationView.frame.size.height/2);
            CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, cellAnimationView.frame.origin.y, endPoint.x, cellAnimationView.frame.origin.y, endPoint.x, endPoint.y);
            pathAnimation.path = curvedPath;
            CGPathRelease(curvedPath);
            CABasicAnimation *basic=[CABasicAnimation animationWithKeyPath:@"transform"];

            [basic setToValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.05, 0.05, 0.25)]];
            [basic setAutoreverses:NO];
            [basic setDuration:0.65];

            [cellAnimationView.layer addAnimation:pathAnimation forKey:@"curveAnimation"];
            [cellAnimationView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.65];
        }
    }
}