2013-06-05 2 views
1

두 점을 연결하는 간단한 선을 그려야합니다. 내 drawLine 메서드에서 그 코드에 맞는 코드가 있다고 생각합니다. 선이 전혀 나타나지 않으며 나는 내가 부르고있는 뷰와 내가 그려야하는 뷰와 관련이 있다는 것을 확신합니다. 다음 코드는 코드에서 다음 뷰 (웹 서비스에서 오는 데이터)를 빌드하는 위치입니다. 뷰는 잘 생성되고 모든 버튼과 객체가 나타납니다.두 점 사이의 선 그리기 - 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를 호출해도 아무런 오류가 발생하지 않지만 아무 것도하지 않습니다. 나는 그 작은 세부 사항을 놓쳤다.

이 문제를 해결하는 데있어 복잡성을 더하는 것은이 모든 것을 프로그래밍 방식으로 만들고 처음에는 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