【问题标题】:CALayer as SubLayer Not VisibleCALayer 作为子层不可见
【发布时间】:2012-07-21 20:17:11
【问题描述】:

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

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

到目前为止我所做的是:

在 AnimateArc.h 中

@interface AnimateArc : CALayer {

CAShapeLayer *circle;
}

-(void) animatingArc;

@end

在 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); }

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

-(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"]; 
}

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

//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; }

最后在我的 viewController 中,

- (void)viewDidLoad
{
[super viewDidLoad];
[self.view.layer addSublayer:AnimateArcObject];
[AnimateArcObject animatingArc];
 }

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

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

【问题讨论】:

    标签: ios4 core-animation invisible quartz-core


    【解决方案1】:

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

    因此,您的图层未显示,因为您缺少以下内容:

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

    您应该在实例化层后立即放置它,并养成这样做的习惯,这样您就不会再次陷入其中。

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

    【讨论】:

    • 感谢您的回复。但是设置界限仍然没有解决我的问题:(
    • 它帮助了我,但没有完全解决它。该图层现在正在屏幕上绘制,但即使我将图层的边界设置为父视图框架,也会与父视图/图层略有偏移。我似乎缺少一些基本的东西。
    • @tracicot 你必须将层的边界设置为原点(0, 0)。然后,使用 center 属性来改变位置。也可以直接设置图层框架来控制位置。
    • 谢谢,@Mazyod。我已经按照您所说的发送了边界,并且框架与父视图相同,以控制图层的位置,但仍然没有运气。
    • 谢谢。这是救命稻草。
    【解决方案2】:

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

    然后在viewDidLoad 方法中,我从this link 编写了代码来使用路径创建圆和动画。

    父视图控制器中,我添加了AnimatedArc ViewController's subview。现在它工作得很好:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多