2013-06-05 69 views
1

我想繪製一條連接兩點的簡單線條。我想我在我的drawLine方法中得到了正確的代碼。根本沒有線條出現,我非常確定它與我打來的視圖以及我想要繪製的視圖有關。以下代碼是我在代碼中構建下一個視圖的數據(來自Web服務的數據)。視圖被創建得很好,所有的按鈕和對象都會出現。在兩點之間劃一條線 - 如何通過UIView

我想添加行到我動態創建(下一個)視圖,不知道如何做到這一點。從我得到的錯誤(無效的上下文0x0)看來,我正在做一些不按順序的事情。嘗試顯示不存在的視圖。我怎麼能做到這一點?任何意見,將不勝感激。

我意識到傳遞視圖可能不是正確的答案。我正處於試驗和錯誤之中。

澄清:我在這裏呼籲的drawLine:

[nextView.view addSubview:未來]。

感謝,

夏嘉曦

-(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); 
} 

回答

1

我最終搞清楚這件事天去,我想我會分享在這裏。我可能在我的問題中添加了很多代碼,但這無助於將其簡化爲其他代碼(無評論)。

這是什麼引起的是,當試圖繪製一條線,你需要重寫UIView子類的DrawRect方法,而不是像我那樣重寫UIViewController。如果從UIViewController調用DrawRect,但沒有任何操作,則不會拋出Error。我錯過了那個小細節。

增加了解決這個問題的複雜性並解決了這個問題,我創建了它的全部內容,並且最初沒有將UIView繼承到子類。我最終在NSMutablearray中添加了所有點,並在新創建View後創建了以下類中的行:

#import "draw.h" 

@implementation draw 
@synthesize lines; 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
if (self) 
{ 
// Initialization code 
} 
return self; 
} 

-(void)drawRect:(CGRect)rect 
{ 

CGContextRef context = UIGraphicsGetCurrentContext(); 

for (line *myLine in lines) 
{ 

    if ([myLine.color isEqualToString:@"255"]) 
      CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
    else if ([myLine.color isEqualToString:@"65535"]) 
       CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor); 
    else if ([myLine.color isEqualToString:@"GRAY"]) 
       CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor); 
    else if ([myLine.color isEqualToString:@"BLACK"]) 
       CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); 
    int setNew = myLine.Width/4; 

    CGContextSetLineWidth(context, (CGFloat)setNew); 
    CGContextBeginPath(context); 
    CGContextMoveToPoint(context, myLine.x1, myLine.y1); 
    CGContextAddLineToPoint(context, myLine.x2, myLine.y2); 
    CGContextStrokePath(context); 
} 
} 
@end 
相關問題