我正在尝试为UIButton的图像设置动画,该图像位于我的UITableViewCell中,为此我需要获取此按钮的当前位置,我将如何设置得到这个?
这是我的代码,但是每当动画从同一个位置开始时,它应该从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];
}
答案 0 :(得分:1)
在下面添加代码以获取nslog中的按钮框
- (void)addToCartTapped:(NSIndexPath*)indexPath {
// grab the cell using indexpath
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIImage *btnImage;
CGRect btnImage_rect;
for (UIButton *btn in [cell.contentView subviews])
{
if ([btn isKindOfClass:[UIButton class]])
{
btnImage =[btn imageForState:UIControlStateNormal];
btnImage_rect.frame=btn.frame;
NSLog(@"rect is %@", btnImage_rect);
}
}
//added your own code here .after you will get button frame
}
答案 1 :(得分:-1)
如果您添加一个UITableViewCell按钮,那么当您尝试通过执行以下操作来获取它的位置时:
button.position.x;
它可能是UITableViewCell中的位置而不是全局位置。