IOS 에서 CALayer 가 그림 을 그 리 는 실례 상세 설명

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); 
} 
궁금 한 점 이 있 으 시 면 메 시 지 를 남기 거나 본 사이트 의 커 뮤 니 티 에 가서 토론 을 교류 하 세 요.읽 어 주 셔 서 감사합니다. 도움 이 되 셨 으 면 좋 겠 습 니 다.본 사이트 에 대한 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기