2017-02-24 70 views
0

我在objective-C中为iOS创建了一个滑球游戏,其中您滑动屏幕并移动球。我已经设置好了,让球沿着你刷卡的方向相反的方向移动。例如,如果我向下滑动并释放,球就会向上移动。在iOS中使用TouchesMoved绘制CGContext Line

我创建了一行来显示使用CGContext移动球的方向。该线从球的中心(ball.center.x,ball.center.y)开始到用手指滑动的点(movePoint)。当我滑动屏幕时,我试图让它画出来,而不是让它画一条线来移动Point,它会在相反的方向画一条线。例如,如果我在屏幕上向下滑动,则会向上绘制一条线。

我已经试过摆弄坐标系,但我似乎无法正确地指向与我滑动的方向相反的方向。有什么特别的方法可以解决这个问题吗?

谢谢。

这是我touchesMoved功能:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    movePoint = [touch locationInView:self.view]; 

    if (CGRectContainsPoint([ball frame], firstPoint)) { 
     _contextImage.hidden = NO; 
     UIGraphicsBeginImageContext(self.view.frame.size); 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
     CGContextSetLineWidth(context, 2); 
     CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); 
     CGContextMoveToPoint(context, ball.center.x, ball.center.y); 
     CGContextAddLineToPoint(context, movePoint.x, movePoint.y); 
     CGContextStrokePath(context); 

     self.contextImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 
} 
+0

我希望这个链接可能会帮助你http://stackoverflow.com/questions/25150674/drawing-with-cgcontext –

回答

0

好了,从球的中心到触摸位置Δ-X将movePoint.x - ball.center.x。另一种方法是,触摸位置的X坐标是ball.center.x + (movePoint.x - ball.center.x),对不对?那么,相反方向的点是通过减去而不是加上(或者换句话说,通过否定Δ-X)来计算的:ball.center.x - (movePoint.x - ball.center.x)

Y位置也是如此:ball.center.y - (movePoint.y - ball.center.y)

所以,画你的线路到那里。