Tôi cố gắng để xây dựng một vòng tròn hoạt hình đó sẽ được rút ra chiều kim đồng hồ cho đến khi nó trở thành vòng tròn hoàn chỉnh như minh họa trong iPhone Core Animation - Drawing a CircleCALayer như lớp con Không Visible
Vấn đề là CALayer
đối tượng không được thêm hoặc xây dựng. Tôi đã kiểm tra và thấy rằng nó không truy cập các phương thức drawInContext:CGContextRef
và animatingArc
của tôi.
gì cho đến nay tôi đã làm là:
Trong AnimateArc.h
@interface AnimateArc : CALayer {
CAShapeLayer *circle;
}
-(void) animatingArc;
@end
Trong 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; }
Và cuối cùng trong viewController tôi,
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view.layer addSublayer:AnimateArcObject];
[AnimateArcObject animatingArc];
}
Apology để định dạng xấu .... Xin ai đó có thể cho tôi biết tôi đang làm gì sai? Tôi cũng nghi ngờ rằng mã của tôi có thể sụp đổ ở bất cứ nơi nào sau khi truy cập hai chức năng kể từ khi tôi mới làm quen với Core Animation và không có bất kỳ ý tưởng rằng tôi đang đi đúng hướng hay không.
Mọi trợ giúp sẽ được đánh giá cao. Cảm ơn.
Cảm ơn câu trả lời của bạn.Nhưng thiết lập giới hạn vẫn không giải quyết vấn đề của tôi: ( – NightFury
Nó đã giúp tôi, nhưng không hoàn toàn giải quyết nó. Lớp này hiện đang vẽ trên màn hình, nhưng hơi lệch từ khung nhìn/lớp gốc ngay cả khi tôi đặt giới hạn của lớp khung nhìn cha mẹ Tôi đang thiếu một cái gì đó cơ bản có vẻ như – tracicot
@tracicot bạn __must__ thiết lập giới hạn của lớp để xuất xứ '(0, 0)' .Sau đó, sử dụng tài sản trung tâm để thay đổi vị trí. Bạn cũng có thể thiết lập các lớp khung trực tiếp để kiểm soát vị trí – Mazyod