iOS CGContext 画图在 UIView和CALayer用相同代码画出来之后线有差异?
PHPz
PHPz 2017-04-18 09:27:50
0
2
337

这是图,上面的是用UIView,下面的使用CALayer。明显Layer上面有锯齿。这种个情况如何解决呢?

代码如下

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 10);
CGContextSetLineJoin(ctx, kCGLineJoinRound);
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);

CGContextBeginPath(ctx);

NSArray *xs = @[@(10),@(35),@(60),@(150),@(80)];
NSArray *ys = @[@(100),@(35),@(40),@(200),@(300)];

CGPoint previousCenter = CGPointZero;
for (NSUInteger i = 0; i < [xs count]; i++)
{
    CGPoint start = previousCenter;
    CGPoint end = CGPointMake([xs[i]floatValue], [ys[i]floatValue]);
    if (i == 0) {
        CGContextMoveToPoint(ctx, start.x, start.y);
    }
    
    CGContextAddLineToPoint(ctx, end.x, end.y);
    
    previousCenter = end;
}

CGContextDrawPath(ctx, kCGPathStroke);

UIView是写在 drawRect:(CGRect)rect里面
CALayer是写在 drawInContext:(CGContextRef)ctx里面

PHPz
PHPz

学习是最好的投资!

모든 응답(2)
Peter_Zhu

CAshapleLayer로 변경할 수 있습니다. 다음은 iphone6 ​​시뮬레이터를 100으로 확대하는 효과에 대한 이미지 코드입니다

으아악

巴扎黑

으아악

DrawLayer에서 위의 값을 설정하세요.

으아악

이 속성의 기본값은 1입니다.

참조 코드:

하위 레이어

으아악

레이어

으아악 으아악

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!