问题描述
我使用 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;
}
输出将是这样的。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)