我试图在一个对象和用户触摸的位置之间画一条线。我已经尝试过子类化,但每次用户触摸屏幕时,我都无法让“-(void)drawrect”自行更新。我删除了这些文件并尝试将代码直接放入“-(void)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());
}