CALayer作为SubLayer不可见

Nig*_*ury 5 invisible core-animation ios4 quartz-core

我正在尝试构建一个动画圆圈,它将顺时针绘制,直到它变成完整的圆圈,如iPhone核心动画 - 绘制圆圈所示

问题是CALayer没有添加或构建对象.我测试过并发现它没有访问我drawInContext:CGContextRefanimatingArc方法.

到目前为止我所做的是:

在AnimateArc.h中

@interface AnimateArc : CALayer {

CAShapeLayer *circle;
}

-(void) animatingArc;

@end
Run Code Online (Sandbox Code Playgroud)

在AnimateArc.m中

-(void) drawInContext:(CGContextRef)ctx
{
CGFloat radius = 50.0;
circle = [CAShapeLayer layer];

//make a circular shape
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.0, 0.0, 2 * radius, 2 * radius) cornerRadius:radius].CGPath;

    CGPoint centerPoint = CGPointMake(CGRectGetWidth(self.bounds)/2, CGRectGetHeight(self.bounds)/2);    

//center the shape in self.view
circle.position = centerPoint;

//configure appearence of circle
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor blackColor].CGColor;
circle.lineWidth = 5;                                           

/*CGPointMake((self.contentsCenter.size.width), (self.contentsCenter.size.height));*/

//path the circle
CGContextAddArc(ctx, centerPoint.x, centerPoint.y, radius, 0.0, 2 * M_PI, 0);
CGContextClosePath(ctx);

//fill it
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextFillPath(ctx); }
Run Code Online (Sandbox Code Playgroud)

////////////////////////////////////////////////// /////////////////////

-(void) animatingArc
{
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"arcEnd"];
anim.duration = 20.0; //animate over 20 seconds
anim.repeatCount = 1.0; //animate only once
anim.removedOnCompletion = NO; //Reamin there after completion

//animate from start to end
anim.fromValue = [NSNumber numberWithFloat:50.0f];
anim.toValue = [NSNumber numberWithFloat:150.0f];

//experiment with timing to get appearence to look the way you want
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

//add animation to circle
[circle addAnimation:anim forKey:@"animatingArc"]; 
}
Run Code Online (Sandbox Code Playgroud)

/////////////////////

//needed since key not part of animatable properties
+(BOOL) needsDisplayForKey:(NSString *)key
{
if([key isEqualToString:@"arcEnd"])
    return YES;
else
    return [super needsDisplayForKey:key];

}

//ensure custom properties copied to presentation layer
-(id) initWithLayer:(id)layer
{
if((self = [super initWithLayer:layer]))
{
    if ([layer isKindOfClass:[AnimateArc class]])
    {
        AnimateArc *other = (AnimateArc *) layer;
        [other setNeedsDisplay];
    }
}
return self; }
Run Code Online (Sandbox Code Playgroud)

最后在我的viewController中,

- (void)viewDidLoad
{
[super viewDidLoad];
[self.view.layer addSublayer:AnimateArcObject];
[AnimateArcObject animatingArc];
 }
Run Code Online (Sandbox Code Playgroud)

对格式错误道歉....请有人告诉我,我做错了什么?我也怀疑我的代码在访问这两个函数之后可能在任何地方崩溃,因为我是Core Animation的新手,并且我不知道我是在正确的方向.

任何帮助将不胜感激.谢谢.

Maz*_*yod 18

根据我对CoreAnimation的痛苦经历,您必须始终设置任何实例化的bounds属性. CALayer

所以,你的图层没有显示,因为你遗漏了以下内容:

layer.bounds = CGRectMake(0, 0, width, height);

你应该在实例化图层时放置它,并养成这样做的习惯,这样你就不会再陷入其中.

至于你的代码崩溃,抱歉.它太分散了,我不确定它是如何联系在一起的,所以我无法帮助你.

  • @tracicot你__must__将图层的边界设置为原点`(0,0)`.然后,使用center属性更改位置.您还可以直接设置图层框以控制位置. (3认同)
  • 谢谢你.这是一个救星. (3认同)
  • 它帮助了我,但没有完全解决它.现在,图层在屏幕上绘制,但与父视图/图层稍微偏移,即使我将图层的边界设置为父视图框架也是如此.我似乎缺少一些基本的东西. (2认同)

Nig*_*ury 0

经过一番搜索后,我认为我走错了方向。所以我删除了这个AnimateArc文件并添加了继承UIViewController.

然后在方法中,我从此链接viewDidLoad编写了代码 ,以使用路径创建圆圈和动画。

父视图控制器中,我添加了AnimatedArc ViewController's subview. 现在它工作完美:)