微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在单个画布上使用 CGContext 更改方形绘制的框架?

如何解决在单个画布上使用 CGContext 更改方形绘制的框架?

我使用 cgcontext 在单个画布上绘制了多个正方形,但现在我需要通过拖动线来修改他的外观。但我找不到任何相同的东西。 这是我的绘图代码

-(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetlinewidth(context,2.0);
    CGContextSetstrokeColorWithColor(context,[UIColor blueColor].CGColor);
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    CGFloat scaleFactor = 1; //self.bounds.size.width/self.bounds.size.height;
    // scale and translate to the standard cartesian coordinate system where the (0,0) is the center of the screen.
    CGContextScaleCTM(context,1,-1);
    CGContextTranslateCTM(context,width*0.5,-height*0.5);
    
    CGContextSetstrokeColorWithColor(context,[UIColor redColor].CGColor);
    CGContextSetlinewidth(context,2.0);
    
//    // add y-axis
    CGContextMovetoPoint(context,-height*0.5);
    CGContextAddLinetoPoint(context,height*0.5);

    // add x-axis
    CGContextMovetoPoint(context,-width*0.5,0);
    CGContextAddLinetoPoint(context,0);
    CGContextSaveGState(context);
    [self applyCTMTransformsForContext:context frame:self.bounds];
    for (Shape *currentShape in self.shapesArray ) {
        BOOL isFirstPoint = YES;
        Points *firstPoint = nil;
        for (Points *point in currentShape.arrayOfPoints) {
            //NSLog(@"X:%f,Y:%f",point.x,point.y);
            if (isFirstPoint) {
                CGContextMovetoPoint(context,point.x/scaleFactor,point.y/scaleFactor);
                //[bpath movetoPoint:CGPointMake(point.x,point.y)];
                isFirstPoint = NO;
                firstPoint = point;
                continue;
            }
            CGContextAddLinetoPoint(context,point.y/scaleFactor);
            
            //[bpath addLinetoPoint:CGPointMake(point.x,point.y)];
            
        }
        CGContextAddLinetoPoint(context,firstPoint.x/scaleFactor,firstPoint.y/scaleFactor);
    }
    CGPathDrawingMode mode = kCGPathstroke;
    CGContextClosePath( context );
    CGContextDrawPath( context,mode );
    CGContextRestoreGState(context);
    //[self drawInContext:UIGraphicsGetCurrentContext()];
}

- (void)applyCTMTransformsForContext:(CGContextRef)context frame:(CGRect)frame
{
    // This is the actual size of the drawing,ideal it will be 2 times biggest x and 2times biggest y
    CGSize graphicSize = CGSizeMake(self.canvasWidth,self.canvasHeight);
    CGSize viewSize = frame.size;
    
    // Translate by the origin of the frame to begin with.
    CGContextTranslateCTM(context,frame.origin.x,frame.origin.y);
    
    switch(self.contentMode)
    {
        case UIViewContentModeScaletoFill:
            // Simple scale
            CGContextScaleCTM(context,viewSize.width/graphicSize.width,viewSize.height/graphicSize.height);
            break;
        case UIViewContentModeScaleAspectFit:
        {
            CGFloat scale = MIN(viewSize.width/graphicSize.width,viewSize.height/graphicSize.height);
            CGContextTranslateCTM(context,(viewSize.width-(graphicSize.width*scale))/2.0f,(viewSize.height-(graphicSize.height*scale))/2.0f);
            CGContextScaleCTM(context,scale,scale);
            break;
        }
        case UIViewContentModeScaleAspectFill:     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
        {
            CGFloat scale = MAX(viewSize.width/graphicSize.width,scale);
            break;
        }
        case UIViewContentModeRedraw:              // redraw on bounds change (calls -setNeedsdisplay)
            break;
        case UIViewContentModeCenter:              // contents remain same size. positioned adjusted.
            CGContextTranslateCTM(context,(viewSize.width-graphicSize.width)/2.0f,(viewSize.height-graphicSize.height)/2.0f);
            break;
        case UIViewContentModetop:
            break;
        case UIViewContentModeBottom:
            CGContextTranslateCTM(context,0.0f,viewSize.height-graphicSize.height);
            break;
        case UIViewContentModeLeft:
            break;
        case UIViewContentModeRight:
            CGContextTranslateCTM(context,viewSize.width-graphicSize.width,0.0f);
            break;
        case UIViewContentModetopLeft:
            break;
        case UIViewContentModetopRight:
            CGContextTranslateCTM(context,0.0f);
            break;
        case UIViewContentModeBottomLeft:
            CGContextTranslateCTM(context,viewSize.height-graphicSize.height);
            break;
        case UIViewContentModeBottomright:
            CGContextTranslateCTM(context,viewSize.height-graphicSize.height);
            break;
    }
}

形状.m

-(instancetype) initWithShapeData:(NSDictionary*) shapeDictionary
{
    self = [super init];
    if (!self) {
        return nil;
    }
    _arrayOfPoints = [NSMutableArray array];

    NSArray *points = shapeDictionary[@"Points"];
    for (int i=0; i<points.count; i++) {
        NSDictionary * dict = points[i];
        
        Points *point = [[Points alloc] init];
        point.x = [dict[@"X"] floatValue];
        point.y = [dict[@"Y"] floatValue];
        [_arrayOfPoints addobject:point];
    }

    return self;
}

输出将是这样的。

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。