Ios 滚动时,动画子视图移除冻结 我的代码:
我正在使用以下代码进行“添加到篮子”动画:Ios 滚动时,动画子视图移除冻结 我的代码:,ios,objective-c,animation,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 = (UIIm
-(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....
});
如果我记得的话,我相信我也尝试了一个带有计时器的运行循环,以及在主线程上执行选择器
我也试着把这个方法放在一个块中(不是我使用的实际代码,只是一个例子):
问题是:
我可以推断滚动占用了主线程,因此我尝试异步调用该方法
有人能告诉我我做错了什么吗?要解决这个问题,您可以使用上面所示的块的完整部分。当时我只是不正确地使用了积木 然而,我解决这个问题的方法是,将动画的代理设置为self,因为我无法使路径动画在块中工作 并使用以下各项:
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
[theView removeFromSuperview];
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
[theView removeFromSuperview];
}