滚动时动画子视图删除会冻结

时间:2016-01-04 00:25:00

标签: ios objective-c animation

我的代码:

我使用以下代码添加到购物篮动画:

-(void)triggerAddItemAnimation:(NSIndexPath*)indexPath
{
    //Cell that was pressed.
    CellMenuItem *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    // grab the imageview using cell
    UIImageView *imgV = (UIImageView*)[cell viewWithTag:IDENTIFIER_ANIMATION_IMAGE];

    //Get the x-coordinate of the image.
    int xCoord = cell.imgAddButton.frame.origin.x;

    // get the exact location of image
    CGRect rect = [imgV.superview convertRect:imgV.frame fromView:nil];
    rect = CGRectMake(xCoord, (rect.origin.y*-1)-10, imgV.frame.size.width, imgV.frame.size.height);

    //NSLog(@"rect is %f,%f,%f,%f",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);

    // create new duplicate image
    UIImageView *starView = [[UIImageView alloc] initWithImage:imgV.image];
    [starView setFrame:rect];
    starView.layer.cornerRadius=5;
    starView.layer.borderColor=[[UIColor blackColor]CGColor];
    starView.layer.borderWidth=1;
    [self.view addSubview:starView];

    // begin ---- apply position animation
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.duration=0.65;
    pathAnimation.delegate=self;

    // tab-bar right side item frame-point = end point
    CGPoint endPoint = CGPointMake(self.lblBasketItemsCount.frame.origin.x, self.lblBasketItemsCount.frame.origin.y);

    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGPathMoveToPoint(curvedPath, NULL, starView.frame.origin.x, starView.frame.origin.y);
    CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, starView.frame.origin.y, endPoint.x, starView.frame.origin.y, endPoint.x, endPoint.y);
    pathAnimation.path = curvedPath;
    CGPathRelease(curvedPath);
    // end ---- apply position animation

    float animationDuration = 0.65f;

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

    [starView.layer addAnimation:pathAnimation forKey:@"curveAnimation"];
    [starView.layer addAnimation:basic forKey:@"transform"];

    //Remove the animation 0.05 before the animationDuration to remove clipping issue.
    [starView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:animationDuration - 0.05f];
}

问题:

动画效果很好但是当我滚动用于动画的图像(从单元格中取出)时,在滚动停止之前不会从视图中删除。

使用方法中的最后一行代码删除要删除的子视图:

 [starView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:animationDuration - 0.05f];

我尝试了什么:

我尝试过使用GCD:

dispatch_async(dispatch_get_main_queue(), ^{
           //call method here....
        });

如果我记得我相信我也尝试过使用计时器的运行循环,以及在主线程上执行选择器。

我也尝试将方法放在一个块中(不是我使用的实际代码,仅仅是一个例子):

 [UIView animateWithDuration:0.5
                          delay:1.0
                        options: UIViewAnimationCurveEaseOut
                     animations:^{

                     } 
                     completion:^(BOOL finished){
                      //Remove subview here
                     }];

问题:

我可以推断出滚动正在占用主线程,因此我试图异步调用该方法。

有人能让我知道我做错了吗?

1 个答案:

答案 0 :(得分:0)

要解决此问题,您可以使用块的完整部分,如上所示。我当时只是错误地使用了这个块。

然而,我解决这个问题的方式,因为我无法在块中使用路径动画来将动画的委托设置为自己。

并使用以下内容:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    [theView removeFromSuperview];
}