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];
}