CALayer作为SubLayer不可见 [英] CALayer as SubLayer Not Visible
问题描述
我正在尝试构建一个动画圆圈,它将顺时针绘制,直到它变成完整的圆圈,如 iPhone核心动画 - 绘制一个圆圈
I am trying to built an animated circle which would be drawn clockwise until it becomes complete circle as illustrated in iPhone Core Animation - Drawing a Circle
问题是 CALayer
对象不是添加或构建。我测试并发现它没有访问我的 drawInContext:CGContextRef
和 animatingArc
方法。
Problem is that CALayer
object is not added or build. I tested and saw that it is not accessing my drawInContext:CGContextRef
and animatingArc
methods.
到目前为止,我所做的是:
What so far I have done is:
在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中,
And finally in my viewController,
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view.layer addSublayer:AnimateArcObject];
[AnimateArcObject animatingArc];
}
对格式错误的道歉......请有人能告诉我我是做什么的做错了?我也怀疑我的代码在访问这两个函数后会在任何地方崩溃,因为我是Core Animation的新手并且不知道我是否正确的方向。
Apology for bad formatting.... Please can someone tell me what am I doing wrong? I have also doubt that my code can crash at any place after accessing those two functions since I am novice about Core Animation and haven't got any idea that I am in right direction or not.
任何帮助将不胜感激。谢谢。
Any help will be appreciated. Thanks.
推荐答案
经过一番搜索,我以为我的方向错了。所以我删除了这个 AnimateArc
文件并添加了一个继承自 UIViewController
。
After little searching, I thought that I am going in wrong direction. So I deleted this AnimateArc
file and added new one which is inheriting from UIViewController
.
然后在 viewDidLoad
方法中,我从此链接使用路径创建圆圈和动画。
Then in viewDidLoad
Method, I wrote the code from this link to create circle and animations using path.
在父级中查看控制器,我添加了 AnimatedArc ViewController的子视图
。现在它完美地工作:)
In parent view controller, I added AnimatedArc ViewController's subview
. Now its working perfectly :)
这篇关于CALayer作为SubLayer不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!