1

我正在尝试为位于我的 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];
}
4

2 回答 2

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    
}
于 2013-11-10T16:40:41.060 回答
-1

如果您添加一个 UITableViewCell 按钮,那么当您尝试通过执行以下操作来获取它的位置时:

 button.position.x;

它可能是它在 UITableViewCell 中的位置,而不是全局位置。

于 2013-11-10T16:24:10.853 回答