2015-04-20 21 views
0

我一直在做循環控制,我做得很好,除了當我第一次進行渲染時,圖形從左上角出現。自定義控件左上角出現的iOS圖形

整個控件的子類型爲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); 
} 

這裏有一個問題video

如果仔細觀察,您會注意到以某種方式渲染圓圈並不會開始居中。它從左上角傾斜。 這隻會在首次執行動畫時發生。

我知道這可能發生,如果一個錯誤開始和結束的動畫提交塊,但我不使用它們在這裏。

萬一這裏是鏈接到這個控件的到位桶repo

回答

2

沒有什麼不對的繪製方法 - 你搞砸了一點點設置層的框架;-)

你在- (void) setAngle:(CGFloat)angle方法中設置circularControlLayer的框架。這意味着當您爲角度屬性設置動畫時,第一次設置了框架 - 所以框架也會動畫。在- (void) commonInitDXCircularControlView方法中設置框架。

如果您正在創建自定義圖層,請查看[UIView layerClass]方法。使用它可以幫你避免界限/框架管理的麻煩。

+0

爲了使它工作,我把它放在'layoutSubviews'方法中。也許是因爲autolayout ..謝謝:) –