[英]Drawing CGContext Line with TouchesMoved in iOS
我正在Objective-C中为iOS创建一个擦球游戏,您可以在该屏幕上滑动屏幕并移动球。 我将其设置为使球朝滑动位置的相反方向移动。 例如,如果我向下滑动并释放,则球会向上移动。
我创建了一条线来显示使用CGContext球移动的方向。 该线从球的中心(ball.center.x,ball.center.y)开始到您用手指滑动的点(movePoint)。 我正在尝试使用它,因此当我滑动屏幕时,与其在movePoint上画一条线,不如在相反的方向上画一条线。 例如,如果我在屏幕上向下滑动,则会向上画一条线。
我尝试摆弄坐标系,但似乎无法正确指向与滑动位置相反的方向。 有什么特别的方法可以解决吗?
谢谢。
这是我的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();
}
}
好吧,从球的中心到触摸位置的增量X是movePoint.x - ball.center.x
。 另一种表示方式是触摸位置的X坐标是ball.center.x + (movePoint.x - ball.center.x)
,对吗? 好吧,相反方向的点是通过减去而不是加(或换句话说,通过减去delta-X)来计算的: ball.center.x - (movePoint.x - ball.center.x)
。
Y位置也是如此: ball.center.y - (movePoint.y - ball.center.y)
。
因此,在那画线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.