2013-11-28 17 views
0

我想在我的UITableViewCell中顯示動畫圈。我已經得到了以下代碼爲self.view工作,但是當我嘗試將它添加到cell.accessoryview時,它從不繪製。任何人有什麼想法,爲什麼它不顯示?將動畫圈添加到我的UITableViewCell中AccesoryView

- (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

@Shan我真的不應該需要繼承細胞。你能提供其他建議嗎? – Apollo

+1

通過繼承單元格可以在單元格上獲得更多的靈活性,對於單元格內的動畫我更喜歡子類化.. –

回答

1

嘗試增加動畫層cellForRowAtIndexPath:方法內,使細胞的肯定accessoryViewnil。如果它是nil,則添加空白UIView與您的動畫圖層的大小,然後添加圖層。祝你好運!

+0

但我想在用戶按下單元格時開始動畫,所以有一種方法可以訪問子圖層它被放置在accessoryview內部?感謝您的回覆 – Apollo

+0

是的,您可以通過訪問accessoryView圖層的'sublayers'方法來獲取圖層。或者,正如@Shan所建議的那樣,子類爲「UITableViewCell」,並添加適當的方法來動畫圖層,這並不難實現。 –

+0

好吧,謝謝Fahri – Apollo