IOS 에서 CALayer 가 그림 을 그 리 는 실례 상세 설명
CALayer 렌 더 링 내용 도 층.UIImageView 에 비해 이벤트 응답 기능 이 없 으 며 UIImageView 는 관리 내용 입 니 다.
주의사항:delegate 대상 을 사용 하여 프 록 시 방법 을 그 리 는 방법 은 delegate 를 nil 로 설정 해 야 합 니 다.그렇지 않 으 면 이상 crash 가 발생 할 수 있 습 니 다.
CALayer 그림 과 선 효과 그림 그리 기:
코드 예제:
CGPoint position = CGPointMake(160.0, 200.0);
CGRect bounds = CGRectMake(0.0, 0.0, 150.0, 150.0);
CGFloat cornerRadius = 150.0 / 2;
CGFloat borderWidth = 2.0;
//
CALayer *layerShadow = [[CALayer alloc] init];
layerShadow.position = position;
layerShadow.bounds = bounds;
layerShadow.cornerRadius = cornerRadius;
layerShadow.borderWidth = borderWidth;
layerShadow.borderColor = [UIColor whiteColor].CGColor;
layerShadow.shadowColor = [UIColor grayColor].CGColor;
layerShadow.shadowOffset = CGSizeMake(2.0, 1.0);
layerShadow.shadowOpacity = 1.0;
layerShadow.shadowRadius = 3.0;
[self.view.layer addSublayer:layerShadow];
//
CALayer *layerContant = [[CALayer alloc] init];
//
[self.view.layer addSublayer:layerContant];
// 、 ( frame)
layerContant.position = position;
layerContant.bounds = bounds;
//
layerContant.backgroundColor = [UIColor redColor].CGColor;
//
layerContant.cornerRadius = cornerRadius;
// 、
// layerContant.mask = nil;
layerContant.masksToBounds = YES;
// 、
layerContant.borderWidth = borderWidth;
layerContant.borderColor = [UIColor whiteColor].CGColor;
// 、 、 、 、
// layerContant.shadowColor = [UIColor grayColor].CGColor;
// layerContant.shadowOffset = CGSizeMake(2.0, 1.0);
// layerContant.shadowOpacity = 1.0;
// CGMutablePathRef path = CGPathCreateMutable();
// layerContant.shadowPath = path;
// layerContant.shadowRadius = 3.0;
//
layerContant.opacity = 1.0;
// 1
//
// (angle : = *M_PI/180;x 、y 、z ;-1.0~1.0)
// layerContant.transform = CATransform3DMakeRotation(M_PI, 0.0, 0.0, 0.0);
// (0.0~1.0)
// layerContant.transform = CATransform3DMakeScale(0.8, 0.8, 0.8);
//
// layerContant.transform = CATransform3DMakeTranslation(10.0, 1.0, 1.0);
//
[layerContant setContents:[UIImage imageNamed:@"header"].CGImage];
그림 그리 기 표시 방법 2
layerContant.delegate = self;
[layerContant setNeedsDisplay];
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
//
CGContextSaveGState(ctx);
// ,
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextTranslateCTM(ctx, 0.0, -150.0);
//
UIImage *image = [UIImage imageNamed:@"header"];
CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, 150.0, 150.0), image.CGImage);
CGContextRestoreGState(cox);
}
// 、
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
//
//
CGContextSetLineWidth(ctx, 1.0);
//
// CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextSetStrokeColorWithColor(ctx, [UIColor greenColor].CGColor);
// 1
//
CGPoint aPoints[2];
aPoints[0] = CGPointMake(10.0, 50.0);
aPoints[1] = CGPointMake(140.0, 50.0);
// points[] , count
CGContextAddLines(ctx, aPoints, 2);
//
CGContextDrawPath(ctx, kCGPathStroke);
// 2
CGContextSetLineWidth(ctx, 5.0);
CGContextSetStrokeColorWithColor(ctx, [UIColor purpleColor].CGColor);
CGContextMoveToPoint(ctx, 10.0, 60.0); //
CGContextAddLineToPoint(ctx, 140.0, 60.0); //
CGContextStrokePath(ctx); //
//
//
CGContextSetLineWidth(ctx, 2.0);
//
CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
//
CGFloat dashArray[] = {1, 1, 1, 1};
CGContextSetLineDash(ctx, 1, dashArray, 1);
//
CGContextMoveToPoint(ctx, 10.0, 100.0);
//
CGContextAddLineToPoint(ctx, 140.0, 100.0);
//
CGContextStrokePath(ctx);
}
// , crash
- (void)dealloc
{
for (CALayer *layer in self.view.layer.sublayers)
{
if ([layer.delegate isEqual:self])
{
layer.delegate = nil;
}
}
NSLog(@"%@ ~", self);
}
궁금 한 점 이 있 으 시 면 메 시 지 를 남기 거나 본 사이트 의 커 뮤 니 티 에 가서 토론 을 교류 하 세 요.읽 어 주 셔 서 감사합니다. 도움 이 되 셨 으 면 좋 겠 습 니 다.본 사이트 에 대한 지지 에 감 사 드 립 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
IOS에서 ReplayKit 및 RTC 사용 방법응용된 소리와 아나운서의 소리를 포함한다.이 두 가지 수요를 감안하여 우리는 스크린 공유를 하는 생방송에 필요한 미디어 흐름을 간단하게 분석할 수 있다. 만약 우리가 Audio App과 Audio Mic를 두 개의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.