UIColor 및 그래픽 컨텍스트
-
14-11-2019 - |
문제
그래서 와 같은 경로를 그립니다 사용자 정의 UIView가 있습니다.
- (void)drawRect:(CGRect)rect{
[[UIColor blackColor] setStroke];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColor(ctx, CGColorGetComponents(self.color));
CGContextFillPath(ctx);
UIBezierPath *thePath = [UIBezierPath bezierPath];
thePath.lineWidth = _lineWidth;
_center = CGPointMake(rect.size.width, rect.size.height);
[thePath moveToPoint:_center];
[thePath addArcWithCenter:_center radius:rect.size.width startAngle:M_PI endAngle:M_PI+degreesToRadians(_angle)clockwise:YES];
[thePath closePath];
[thePath fill];
}
.
내 사용자 정의 UIView에는 채우기 색상을 변경하는 방법이 있습니다
- (void) changeColor:(UIColor *) newColor {
self.color = [newColor CGColor];
[self setNeedsDisplay];
}
.
changecolor : 와 같은 미리 정의 된 색상의 메소드를 호출하는 경우 :
[UIColor redColor]
.
모든 것이 잘 작동합니다.대신 와 같은 사용자 정의 색상을 주려고합니다.
UIColor* color = [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f];
.
흰색, 빨간색 및 파란색 사이의 맞춤형 UIView 플리커의 색상 무작위로.
왜 그런지 왜 그런지?
미리 감사드립니다!
해결책
시도 :
CGContextSetFillColorWithColor(ctx, self.color.CGColor);
.
대신 :
CGContextSetFillColor(ctx, CGColorGetComponents(self.color));
. 다른 팁
이 메소드를 사용할 때 호출 :
[UIColor redColor];
.
최적화를 위해, UIColor는 다른 하나는 필요 없기 때문에 2 개의 구성 요소만으로 개체를 반환합니다.클래스 클러스터링 메커니즘으로 가능합니다.
코드에 코드 가이 두 가지 색상간에 몇 가지 차이점을 보여주는 이유입니다.
제휴하지 않습니다 StackOverflow