简体   繁体   中英

CGPattern draw not start with desired point

I have a simple code draw square pattern with size PSIZE = 50 on UIView. Every things are ok when I start draw at zero(0,0) point. When I translate context to another point not multiple of PSIZE then the pattern still works but not start with desired point. The following images are result when I translate to point (75, 75) and original point. Can any body help me found the way to start draw at any point with exactly original point of cell pattern.

#define PSIZE 50
void backgroundLinePattern (void *info, CGContextRef context)
{
  UIColor * lineColor = [UIColor yellowColor];

  CGContextSetStrokeColorWithColor(context, lineColor.CGColor);

  CGContextMoveToPoint(context, 0, 0);
  CGContextSetLineWidth(context, PSIZE * 0.1);
  CGContextAddLineToPoint(context, 0, PSIZE);
  CGContextAddLineToPoint(context, PSIZE, PSIZE);
  CGContextAddLineToPoint(context, PSIZE, 0);
  CGContextAddLineToPoint(context, 0, 0);
  //CGContextAddLineToPoint(context, PSIZE, PSIZE);
  CGContextStrokePath(context);
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}

-(void) drawLayer:(CALayer *)layer inContext:(CGContextRef)context{
  //CGContextScaleCTM(context, 10, 10);
  CGContextSaveGState(context);
  CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL);
  CGContextSetFillColorSpace(context, patternSpace);
  CGColorSpaceRelease(patternSpace);
  //CGContextTranslateCTM(context, 75, 75); //TRANSLATE LINE
  //CGAffineTransform t = CGAffineTransformMakeScale(50, 50);
  CGRect r = CGRectMake(-0, -0, PSIZE, PSIZE);
  const CGPatternCallbacks callbacks = { 0, &backgroundLinePattern,NULL };
  CGPatternRef pattern = CGPatternCreate(NULL,
                                         r,
                                         CGAffineTransformIdentity,
                                         PSIZE,
                                         PSIZE,
                                         kCGPatternTilingConstantSpacing,
                                         true,
                                         &callbacks);
  CGFloat alpha = 1.0;
  CGContextSetFillPattern(context, pattern, &alpha);
  CGPatternRelease(pattern);

  CGRect rect = CGRectMake(0, 0, 400, 400);
  CGContextFillRect(context,rect);

  CGContextRestoreGState(context);
}

在此处输入链接说明

在此处输入链接说明

Patterns are repeated over the graphics context as a whole . Your drawing is merely, as it were, a window on that area of the pattern. Thus if you shift the drawing and you want the pattern to start at the same place relative to the shifted drawing, you must shift the start point of the pattern separately. Study CGContextSetPatternPhase() to see how to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM