在我的UITableViewCell AccesoryView中添加动画圆圈

时间:2013-11-28 05:52:43

标签: ios objective-c caanimation

我想在UITableViewCell中显示一个动画圈。我已经获得了以下代码来为self.view工作,但是当我尝试将它添加到cell.accessoryview时它永远不会绘制。谁有任何关于它为什么没有出现的想法?

- (void)makeCircleAtIndexPath:(NSIndexPath *)path {

     UITableViewCell *cell = [_permanentSoundTableView cellForRowAtIndexPath:path];

    // Set up the shape of the circle
    int radius = 10;
    circle = [CAShapeLayer layer];
    // Make a circular shape
    circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
                                             cornerRadius:radius].CGPath;
    // Center the shape in self.view
    circle.position = CGPointMake(cell.frame.size.width-10,cell.frame.size.height-5);

    // Configure the apperence of the circle
    circle.fillColor = [UIColor clearColor].CGColor;

    UIColor *transparentBlack = [UIColor colorWithRed:250.0f green:250.0f blue:250.0f alpha:.5];

    circle.strokeColor = transparentBlack.CGColor;
    circle.lineWidth = 5;


    // Add to parent layer
    [cell.accessoryView.layer addSublayer:circle];

    // Configure animation
    CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    drawAnimation.duration            = 8.0; // "animate over 10 seconds or so.."
    drawAnimation.repeatCount         = 1.0;  // Animate only once..
    drawAnimation.removedOnCompletion = NO;   // Remain stroked after the animation..

    // Animate from no part of the stroke being drawn to the entire stroke being drawn
    drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    drawAnimation.toValue   = [NSNumber numberWithFloat:1.0f];

    // Experiment with timing to get the appearence to look the way you want
    drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    // Add the animation to the circle
    [circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
}

1 个答案:

答案 0 :(得分:1)

尝试在cellForRowAtIndexPath:方法中添加动画图层,并确保该单元格的accessoryView不是nil。如果是nil,则添加动态图层大小的空UIView,然后添加图层。祝你好运!