2013-02-25 3 views
-1

사용자가 화면의 아무 곳이나 클릭 할 때 원을 그려야합니다. 이 코드가 없거나 잘못 되었습니까?석영을 사용하여 클릭하면 원을 그립니다.

- (void)drawRect:(CGRect)rect 
{ 
if (UITouchPhaseBegan) 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(context, 0, 0, 225, 1); 
    CGContextSetRGBFillColor(context, 0, 0, 255, 1); 
    CGRect rectangle = CGRectMake(50, 50, 500, 500); 
    CGContextStrokeEllipseInRect(context, rectangle); 
} 

} 

답변

1

코드는 생각대로 작동하지 않습니다. UITouch.hUITouchPhaseBegan의 정의를 살펴 유무 :

typedef NS_ENUM(NSInteger, UITouchPhase) { 
    UITouchPhaseBegan,    // whenever a finger touches the surface. 
    UITouchPhaseMoved,    // whenever a finger moves on the surface. 
    UITouchPhaseStationary,  // whenever a finger is touching the surface but hasn't moved since the previous event. 
    UITouchPhaseEnded,    // whenever a finger leaves the surface. 
    UITouchPhaseCancelled,   // whenever a touch doesn't end but we need to stop tracking (e.g. putting device to face) 
}; 

그냥 열거 값이 아니라 앱에 무슨 일이 일어나고 있는지의 반영이다. 이 경우 열거 형의 첫 번째 값이기 때문에 컴파일러에서 0으로 설정하고 있으므로 항상 false로 평가됩니다.

아마 당신이하고 싶은 것은 BOOL _touchHasbegun;과 같은 ivar을 설정하는 것입니다. 그런 다음 -touchesBegan:withEvent 또는 제스처 인식기 동작에서 터치 처리 방법에 따라 _touchHasBegun을 적절하게 YES 또는 NO로 설정하십시오.

보기를 업데이트해야하는 경우 메서드를 트리거하려면 [self setNeedsDisplay] (또는 더 나은 성능을 위해 가능한 경우 [self setNeedsDisplayInRect:someRect])으로 전화하십시오. 그런 다음 -drawRect: 방법으로 _touchHasBegun 여부를 확인하여 서클을 그릴 지 여부를 결정합니다.

참고 : 절대로 -drawRect:으로 전화하지 마십시오. 뷰를 더티로 설정하면 OS가 적절한 시점에 드로잉을 처리합니다.

+0

_touchHasBegun을 사용할 때 -drawRect 메서드를 어떻게 확인합니까? – JimmyYXA

+0

_touchHasBegun 일 때 -drawRect 확인 방법은? – JimmyYXA

+0

'-setNeedsDisplay'와'-setNeedsDisplayInRect :'의 사용법을 포함하도록 답을 수정했습니다. –

관련 문제