한 개체와 사용자가 만난 곳 사이에 선을 그리려합니다. 서브 클래 싱을 시도했는데 사용자가 화면에 닿을 때마다 "- (void) drawrect"를 업데이트 할 수 없습니다. 나는 그 파일을 삭제하고 오른쪽에 코드를 퍼팅 시도 "- (무효) touchesbegan"하지만, 그것은 작동하지 않습니다 필요한 경우사용자가 터치 한 위치에 선을 그리려면 어떻게해야합니까?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
CGPoint locationOfTouch = [touch locationInView:nil];
// You can now use locationOfTouch.x and locationOfTouch.y as the user's coordinates
Int xpos = (int)(starShip.center.x);
int ypos = (int)(starShip.center.y);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), starShip.center.x, starShip.center.y);
//draws a line to the point where the user has touched the screen
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), locationOfTouch.x, locationOfTouch.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
}
감사합니다. 하지만 하위 클래스 또는보기 컨트롤러 drawrect 넣을 수 있습니까? – CompleteXcodeNoob
'drawRect :'는'UIViewController'가 아닌'UIView'의 메소드입니다. – DrummerB
감사합니다. – CompleteXcodeNoob