2011-11-11 3 views
0

UILabel에서 다른 행으로 드래그하는 프로그램을 작성 중입니다. DragView라는 UIView 하위 클래스를 만들고 drawRect 메서드를 오버라이드했습니다. UIViewRootController보기의 하위보기로 만들었습니다. DragView이 명확하게 표시되고 drawRect 메서드가 확실히 호출되고 있지만 선이 표시되지 않습니다.UIPanGestureRecognizer를 사용하여 ios에서 행을 드래그하는 방법

이들은 (내가 생각하기에) 관련 코드 조각입니다.

//DragView.m 

- (void)drawRect:(CGRect)rect 
{ 
    if (context == nil) 
    { 
     context = UIGraphicsGetCurrentContext(); 
    } 

    if (_drawLineFlag) 
    { 

     CGContextSetLineWidth(context,2.0); 
     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 
     CGFloat components[] = {0.0,0.0,1.0,1.0}; 
     CGColorRef color = CGColorCreate(colorspace, components); 

     CGContextSetStrokeColorWithColor(context,color); 

     CGContextMoveToPoint(context,_startX, _startY); 

     CGContextAddLineToPoint(context,_currentX,_currentY); 

     CGContextStrokePath(context); 
    } 
}  

DrawProgramAppDelegate

- (void) initializeUI 
{ 
..... 

    dragView = [[DragView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)] ; 
    [view addSubview: dragView]; 
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; 
    [dragView addGestureRecognizer:panGestureRecognizer]; 
..... 

} 

이벤트 핸들러는 다음과 같습니다 도와

- (void) handlePan:(UIPanGestureRecognizer *) recognizer    
{ 
    CGPoint point = [recognizer translationInView:dragView]; 

    [dragView setCurrentX:point.x]; 
    [dragView setCurrentY:point.y]; 
    [dragView setDrawLineFlag:YES]; 

    [view bringSubviewToFront:drawView]; 
    [dragView drawRect:CGRectMake (0.0f,0.0f,768.0f, 1024.0f)]; 
} 

많은 감사합니다.

존이

답변

1

그것은 당신이 경로를 그릴하려고하는 날 것으로 보인다 (이 post 살펴 보셔야합니다). 여기 방금 단지 작은 선을 그릴 것입니다 전화의 주파수를 주어진 현재 위치

CGContextMoveToPoint(context,_startX, _startY); 
CGContextAddLineToPoint(context,_currentX,_currentY); 

에 마지막 점에서 선을 그릴 것으로 보인다.

당신의 drawRect를 호출해서는 안 :

[dragView drawRect:CGRectMake (0.0f,0.0f,768.0f, 1024.0f)]; 

당신은 또한 당신이

typedef enum { 
    UIGestureRecognizerStatePossible, 
    UIGestureRecognizerStateBegan, 
    UIGestureRecognizerStateChanged, 
    UIGestureRecognizerStateEnded, 
    UIGestureRecognizerStateCancelled, 
    UIGestureRecognizerStateFailed, 
    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded 
} UIGestureRecognizerState; 
으로 정의 된 제스처 인식기 상태를 확인해야합니다

[dragView setNeedsDisplay]; 

를 표시하는 시스템을보기 요구 사항을 알려야합니다

상태가 UIGestureRecognizerStateChanged 또는인 경우에만 그릴 수 있습니다.

+0

어떻게 검색하고 찾을 수 있습니까? 나는 drwan 선을 두 드렸을 때 hightlight하고 싶다. 이 코드베이스 (http://stackoverflow.com/questions/25811490/draw-line-using-uipinchgeurerecognizer). – Sierra

관련 문제