繁体   English   中英

iPhone SDK触摸事件

[英]Iphone SDK touches event

我试图通过编写一些跟踪触摸的划线来更好地理解触摸:

- (void)drawRect:(CGRect)rect {
NSLog (@"My draw");

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

CGContextMoveToPoint(context, prev.x, prev.y);
CGContextAddLineToPoint(context, cur.x, cur.y);
    CGContextStrokePath(context);
return;
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *touch = [touches anyObject];
prev = [touch previousLocationInView:self];
cur = [touch    locationInView:self];

[self setNeedsDisplayInRect:CGRectMake (prev.x, prev.y, fabs (cur.x-prev.x), fabs (cur.y - prev.y) )];
}

很明显,我的setNeedsDisplay是不正确的,因为它仅适用于从正坐标到负坐标(从左上到右下)的移动。 带着这个问题:

  1. 看来我必须分别为4种不同的潜在移动方向情况(从pos X,Y到nex X,Y,从pos X到nex X等)编写setNeedsDisplay。 这是正确的方法,还是我缺少一些基本知识?

  2. 即使进行正确的移动,该线也不是实线,而是断线(取决于手指跟踪的速度)。 为什么实线不能跟踪运动?

谢谢!

setNeedsDisplay不会立即调用drawRect 实际上, touchesMoved可能在下一次绘制视图之前被调用多次,这就是为什么看到虚线的原因。

您对CGRectMake的致电应为:

CGRectMake (MIN(cur.x, prev.x), MIN (cur.y, prev.y), fabs (cur.x-prev.x), fabs (cur.y - prev.y))

在我的代码中,我使用从任意两点组成一个rect的函数对此进行概括,例如:

CGRect rectWithPoints(CGPoint a, CGPoint b)
{
return CGRectMake(
   MIN (a.x, b.x),  
   MIN (a.y, b.y), 
   fabs (a.x - b.x), 
   fabs (a.y - b.y));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM