我正在尝试为位于我的 UITableViewCell 中的 UIButton 的图像设置动画,为此我需要获取此按钮的确切当前位置,我将如何设法获得这个?
这是我的代码,但是当动画每次都从同一个位置开始时,即使它应该从 UITableViewCell 中的 UIButton 开始。
- (void)addToCartTapped:(NSIndexPath*)indexPath {
// grab the cell using indexpath
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIImage *btnImage;
for (UIButton *btn in [cell.contentView subviews])
{
if ([btn isKindOfClass:[UIButton class]])
{
btnImage = [btn imageForState:UIControlStateNormal];
}
}
UIImageView *imgV = [[UIImageView alloc] initWithImage:btnImage];
CGRect rect = [imgV.superview convertRect:imgV.frame fromView:nil];
rect = CGRectMake(5, (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(210+rect.size.width/2, 390+rect.size.height/2);
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
// apply transform animation
CABasicAnimation *basic=[CABasicAnimation animationWithKeyPath:@"transform"];
[basic setToValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.25, 0.25, 0.25)]];
[basic setAutoreverses:NO];
[basic setDuration:0.65];
[starView.layer addAnimation:pathAnimation forKey:@"curveAnimation"];
[starView.layer addAnimation:basic forKey:@"transform"];
[starView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.65];
[self performSelector:@selector(reloadBadgeNumber) withObject:nil afterDelay:0.65];
}