自定义控件左上角显示的iOS图形

自定义控件左上角显示的iOS图形,ios,objective-c,core-graphics,core-animation,Ios,Objective C,Core Graphics,Core Animation,我一直在做一个循环控制,我做得很好,除了图形出现在左上角,当我第一次渲染 整个控件是子类化的UIControl,带有定制的CALayer,用于渲染圆 这是渲染圆的代码: - (void) drawInContext:(CGContextRef)ctx{ id modelLayer = [self modelLayer]; CGFloat radius = [[modelLayer valueForKey:DXCircularControlLayerPropertyNameRadius] floa

我一直在做一个循环控制,我做得很好,除了图形出现在左上角,当我第一次渲染

整个控件是子类化的
UIControl
,带有定制的
CALayer
,用于渲染圆

这是渲染圆的代码:

- (void) drawInContext:(CGContextRef)ctx{
id modelLayer = [self modelLayer];
CGFloat radius = [[modelLayer valueForKey:DXCircularControlLayerPropertyNameRadius] floatValue];
CGFloat lineWidth = [[modelLayer valueForKey:DXCircularControlLayerPropertyNameLineWidth] floatValue];
//NSLog(@"%s, angle: %6.2f, radius: %6.2f, angle_m: %6.2f, radius_m: %6.2f", __func__, self.circleAngle, self.circleRadius, [[modelLayer valueForKey:@"circleAngle"] floatValue], [[modelLayer valueForKey:@"circleRadius"] floatValue]);

// draw circle arc up to target angle

CGRect frame = self.frame;
CGContextRef context = ctx;

CGContextSetShouldAntialias(context, YES);
CGContextSetAllowsAntialiasing(context, YES);

// draw thin circle

//CGContextSetLineWidth(context, <#CGFloat width#>)

// draw selection circle

CGContextSetLineWidth(context, lineWidth);
CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
CGContextAddArc(context, frame.size.width / 2.0f, frame.size.height / 2.0f, radius, 0.0f, self.circleAngle, 0);

CGContextStrokePath(context);

CGContextSetShouldAntialias(context, NO);
}
-(void)drawInContext:(CGContextRef)ctx{
id modelLayer=[self modelLayer];
CGFloat radius=[[modelLayer valueForKey:DXCircularControlLayerPropertyNameRadius]浮点值];
CGFloat线宽=[[modelLayer valueForKey:DXCircularControlLayerPropertyNameLineWidth]浮点值];
//NSLog(@%s,角度:%6.2f,半径:%6.2f,角度μm:%6.2f,半径μm:%6.2f“,u函数μu,self.circleAngle,self.circleRadius,[[modelLayer valueForKey:@“circleAngle”]floatValue],[modelLayer valueForKey:@“circleRadius”]floatValue]);
//绘制圆弧直到目标角度
CGRect frame=self.frame;
CGContextRef context=ctx;
CGContextSetShouldAntialias(上下文,是);
CGContextSetAllowsAntialiasing(上下文,是);
//画圈
//CGContextSetLineWidth(上下文,)
//画选择圈
CGContextSetLineWidth(上下文,线宽);
CGContextSetStrokeColorWithColor(上下文[UIColor greenColor].CGColor);
CGContextAddArc(上下文,frame.size.width/2.0f,frame.size.height/2.0f,半径,0.0f,self.circleAngle,0);
CGContextStrokePath(上下文);
CGContextSetShouldAntialias(上下文,否);
}
下面是一个问题的答案

若你们仔细观察,你们会发现圆的渲染并不是从中心开始的。它从左上角开始倾斜。 这仅在第一次执行动画时发生

我知道如果有人在动画提交块的开始和结束时出错,就会发生这种情况,但我在这里不使用它们


以防万一,这里是指向此控件的bitbucket的链接:

绘图方法没有问题-您在设置层的帧时弄错了一点;-)

您正在使用
-(void)setAngle:(CGFloat)angle
方法设置CircularController层的帧。这意味着在设置“角度”属性的动画时,将首次设置帧-因此帧也将设置动画。在
-(void)commonInitDXCircularControlView
方法中设置帧


如果要创建自定义图层,请查看
[UIView layerClass]
方法。使用它可以避免边界/框架管理方面的麻烦。

为了使它正常工作,我将它放在了
layoutSubviews
方法中。可能是因为自动布局。。谢谢:)