Draw grid in a View iOS || iOS drawing inside gridView

Solution:

Draw a gridView and using the gridView u can easily measure the drawings inside a view.So below code is used to draw a grid in a view ....

#pragma mark - Draw grid
-(void)drawGridRect:(CGRect)rect {
    CGFloat viewWidth = self.frame.size.width;
    CGFloat viewHeight = self.frame.size.height;
    CGFloat gridValue = 10.0;
    
    CGFloat x1 = 0.0;
    CGFloat x2 = viewWidth;
    
    CGFloat y1 = 0.0;
    CGFloat y2 = viewHeight;
    
    CGFloat cellLength = gridValue;
    CGFloat lineWidth = 2.0;
     UIBezierPath *path = [UIBezierPath bezierPath];

    // draw horizontal lines
    CGFloat i = 0.0;
    do {
        [path moveToPoint:CGPointMake(x1, cellLength + i)];
        [path addLineToPoint:CGPointMake(x2, cellLength + i)];
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.path = [path CGPath];
        shapeLayer.strokeColor = [[UIColor lightGrayColorCGColor];
        shapeLayer.lineWidth = 1.0;
        shapeLayer.fillColor = [[UIColor clearColorCGColor];
        [self.layer addSublayer:shapeLayer];

        i = i + cellLength;
    } while (i < rect.size.height - cellLength);
    
    // draw vertical lines
    CGFloat j = 0.0;
    do {
        [path moveToPoint:CGPointMake(cellLength+j, y1)];
        [path addLineToPoint:CGPointMake(cellLength+j, y2)];
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.path = [path CGPath];
        shapeLayer.strokeColor = [[UIColor lightGrayColorCGColor];
        shapeLayer.lineWidth = 1.0;
        shapeLayer.fillColor = [[UIColor clearColorCGColor];
        [self.layer addSublayer:shapeLayer];

        j = j + cellLength;

       } while (j < rect.size.width - cellLength);

}

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift