我正在尝试画一条连接两点的简单线。我想我在 drawLine 方法中得到了正确的代码。根本没有线条出现,我很确定这与我正在调用的视图和我想要绘制的视图有关。以下代码是我在代码中构建下一个视图的地方(数据来自 Web 服务)。视图创建得很好,所有按钮和对象都出现了。
我正在尝试向我正在创建的视图(下一步)动态添加行,但不确定如何完成。从我得到的错误(无效的上下文 0x0)看来,我做的事情有问题。试图在不存在的视图上显示。我怎样才能完成这项工作?任何意见,将不胜感激。
我意识到传递视图可能不是正确的答案。我在这里处于反复试验中。
澄清一下:我在这里调用drawLine:
[nextView.view addSubview:next];
谢谢,
晒
-(void) drawView
{
CGRect rec;
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
UIView *next = [[UIView alloc]initWithFrame:CGRectMake(0, 0, height, width)];
next.backgroundColor = [UIColor whiteColor];
next.userInteractionEnabled = YES;
next.multipleTouchEnabled = YES;
nextView = [UIViewController alloc];
[nextView setTitle: as_objectDesc];
for (object in viewObjects)
{ // ratio based on Ipad 2 screen size conversion from the Datawindow size - alter to support other device
CGFloat objWidth = object.Width/3.83;
CGFloat objHeight = object.Height/2.78;
CGFloat objXPoint = object.positionX/3.83;
CGFloat objYPoint = object.positionY/2.78;
if ([object.Shape isEqualToString:@"text"])
{
rec = CGRectMake(objXPoint, objYPoint, objWidth, objHeight);
UILabel *myLabel = [[UILabel alloc] initWithFrame:rec];
myLabel.backgroundColor = [UIColor clearColor];
myLabel.text = object.objectTag;
myLabel.lineBreakMode = NSLineBreakByWordWrapping;
if ([object.description isEqualToString:@"Header"])
{
myLabel.font = [UIFont systemFontOfSize:20];
}
else
{
myLabel.font = [UIFont systemFontOfSize:10];
}
[next addSubview:myLabel];
}
else if ([object.Shape isEqualToString:@"line"])
{
**[self drawLine:nextView.view];**
}
else
{
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
if ([object.Shape isEqualToString:@"ellipse"])
{
UIImage * buttonImage;
if ([object.Color isEqualToString:@"255"])
{
buttonImage = [UIImage imageNamed:@"redBackground.png"];
}
else if ([object.Color isEqualToString:@"65535"])
{
buttonImage = [UIImage imageNamed:@"yellowBackground.png"];
}
else if ([object.Color isEqualToString:@"GRAY"])
{
buttonImage = [UIImage imageNamed:@"graybground.PNG"];
}
else if ([object.Color isEqualToString:@"BLACK"])
{
buttonImage = [UIImage imageNamed:@"BlackBackground.png"];
}
[myButton setBackgroundImage:buttonImage forState: UIControlStateNormal];
objWidth = objHeight;
rec = CGRectMake(objXPoint, objYPoint, objWidth, objHeight);
myButton.frame = rec;
myButton.clipsToBounds = YES;
myButton.layer.cornerRadius = objWidth/2;
}
else
{
rec = CGRectMake(objXPoint, objYPoint, objWidth, objHeight);
if ([object.Color isEqualToString:@"255"])
myButton.backgroundColor = [UIColor redColor];
else if ([object.Color isEqualToString:@"65535"])
myButton.backgroundColor = [UIColor yellowColor];
else if ([object.Color isEqualToString:@"GRAY"])
myButton.backgroundColor = [UIColor grayColor];
else if ([object.Color isEqualToString:@"BLACK"])
myButton.backgroundColor = [UIColor blackColor];
}
[[myButton layer] setBorderWidth:1.0f];
[[myButton layer] setBorderColor:[UIColor blackColor].CGColor];
myButton.enabled = YES;
myButton.accessibilityHint = object.objectTag;
myButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
myButton.titleLabel.textAlignment = NSTextAlignmentCenter;
myButton.titleLabel.font = [UIFont systemFontOfSize:13];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = rec;
[myButton setTitle:object.description forState:UIControlStateNormal];
// Check if an Empty Rectangle
NSRange test =[myButton.accessibilityHint rangeOfString:@"r_"];
if (test.location == 0)
{
[next sendSubviewToBack:myButton];
[myButton setEnabled:NO];
}
[next addSubview:myButton];
}
UIPinchGestureRecognizer *twoFingerPinch = [[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:@selector(twoFingerPinch:)]
;
[[nextView view] addGestureRecognizer:twoFingerPinch];
[next addSubview:activityIndicator];
[next bringSubviewToFront:activityIndicator];
}
[nextView.view addSubview:next];
[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController pushViewController:nextView animated:NO];
}
- (void)drawLine:(UIView *) view
{
CGFloat x1 = object.positionX;
CGFloat y1 = object.positionY;
CGFloat x2 = object.positionX2;
CGFloat y2 = object.positionY2;
CGContextRef context = UIGraphicsGetCurrentContext();
if ([object.Color isEqualToString:@"255"])
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
else if ([object.Color isEqualToString:@"65535"])
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
else if ([object.Color isEqualToString:@"GRAY"])
CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
else if ([object.Color isEqualToString:@"BLACK"])
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 3.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, x1, y1);
CGContextAddLineToPoint(context, x2, y2);
CGContextStrokePath(context);
}