2011-09-22 5 views
0

처음 사용자 및 iPhone 개발자 초보자. 제 질문은 핵심적인 코드가 아닌 응용 프로그램의 아키텍처에 중점을 둡니다. 내 질문은 : 올바른 길을 가고 있습니까? 아니면이 부분에 대한 나의 접근 방식을 다시 생각해 볼 필요가 있습니까?iPhone Dev - Drawing 응용 프로그램

간단한 "도트 연결"응용 프로그램을 만들려고합니다. 내 응용 프로그램은 손으로 직접 그리는 기능을 가지고 있으며 각 점을 나타 내기 위해 UIButtons를 사용하고 있습니다.

2 개의 UIButtons (도트)의 가운데 속성을 호출하고 시작/끝 CGPoints가이 두 도트의 중심 좌표 인 경우에만 선을 그리는 조건을 설정하여이 문제에 접근했습니다. 이것은 작동하지 않습니다!

그래서 제 질문은 :

이 UIButtons 각 점을 표현하기위한 최선의 방법인가? 그렇다면 각 점에 어떤 기능을 추가해야합니까? 센터 속성을 호출하고 센터 좌표를 얻을 수 있기 때문에 강력한 후보자처럼 보입니다. 그러나 이것으로 문제가 생겨서 하나의 픽셀이 조건을 배치하기에 충분히 크지 않을 것으로 생각했습니다.

UIButtons가 각 도트를 표현하는 최상의 방법이 아닌 경우 더 나은 대안은 무엇입니까?

마지막으로이 문제로 인해 UIButtons의 속성 및 기능을 연구하는 데 많은 시간을 투자했습니다. UIButton을 통해 사용할 수있는 보낸 된 이벤트 옵션에 대한 설명에 대한 좋은 참조를 찾을 수 없습니다. 누구든지 좋은 블로그/참조를 알고 있습니까?

미리 도움을 주셔서 감사합니다.

답변

0

UIButton 도청은 권장되지 않습니다. 다음은 손으로 그림을 그리는 샘플입니다. 대신 UIButtons과 이벤트에 초점을 맞추고의

http://www.ifans.com/forums/showthread.php?t=132024

+0

감사합니다. 우연히 그 응용 프로그램에 대해 언급 한 샘플입니다. 누구든지 UIButton 보낸 이벤트 설명에 대한 블로그/참조를 알고 있습니까? – des253

0

, 나는 UIButtons을 제거하고 touchesBegan, touchesMovedtouchesEnd 방법에 초점을 맞추었다. 특정 초기 위치와 특정 현재 위치에 UITouch이 등록 된 경우에만 선을 그리는 CGContext 메서드를 통합했습니다. 이것이 내 문제에 대한 가장 이상적이고 효율적인 대답인지 확신 할 수는 없지만 잘 작동하고 내 프로젝트의 다음 단계로 넘어갈 수있었습니다. 누구든지이 솔루션을 개선하기위한 제안 사항이 있으면 크게 감사하겠습니다. 다음은이 솔루션에 사용한 코드 스 니펫입니다.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    if([touch tapCount] == 2) { 
     image1_.image = nil; 
     return; 
    } 

    initialPoint = [touch locationInView:self.view]; 

    //If initial touch to screen is within 15 pixels of Dot 1, set coordinates to Dot 1 
    if(initialPoint.x > 36 && initialPoint.x < 66 && initialPoint.y > 161 && initialPoint.y < 191) 
    { 
     initialPoint.x = 51.0; 
     initialPoint.y = 176.0; 
    } 

    //If initial touch to screen is within 15 pixels of Dot 2, set coordinates to Dot 2  
    if(initialPoint.x > 199.5 && initialPoint.x < 229.5 && initialPoint.y > 170.5 && initialPoint.y < 190.5) 
    { 
     initialPoint.x = 214.5; 
     initialPoint.y = 175.5; 
    } 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    currentPoint = [touch locationInView:self.view]; 

    //If current touch to screen is within 15 pixels of Dot 2, and the initial touch is 
    //set to Dot 1, draw line 
    if(currentPoint.x > 199.5 && currentPoint.x < 229.5 && currentPoint.y > 170.5 && currentPoint.y < 190.5 && initialPoint.x == 51.0 && initialPoint.y == 176.0) 
    { 
     currentPoint.x = 214.5; 
     currentPoint.y = 175.5; 

     UIGraphicsBeginImageContext(self.view.frame.size); 
     [image1_.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, 
              self.view.frame.size.height)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapSquare); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 4.0); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0.0, 0.0, 0.0, 1.0); 
     CGContextBeginPath(UIGraphicsGetCurrentContext()); 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), initialPoint.x, initialPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),currentPoint.x, currentPoint.y); 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     image1_.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     lineIsDrawn = YES; 
    }  

    //If current touch to screen is within 15 pixels of Dot 3, and the initial touch is 
    //set to Dot 2, and a line has already been drawn between Dot 1 & Dot 2, draw line  
    if(currentPoint.x > 155.5 && currentPoint.x < 180.5 && currentPoint.y > 0 && currentPoint.y < 28.5 && initialPoint.x == 214.5 && initialPoint.y == 175.5 && lineIsDrawn == YES) 
    { 
     currentPoint.x = 170.5; 
     currentPoint.y = 13.5; 
     UIGraphicsBeginImageContext(self.view.frame.size); 
     [image1_.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, 
              self.view.frame.size.height)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapSquare); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 4.0); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0.0, 0.0, 0.0, 1.0);   
     CGContextBeginPath(UIGraphicsGetCurrentContext()); 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), initialPoint.x, initialPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),currentPoint.x, currentPoint.y); 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     image1_.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    }