-1

我正在画一条直线,并且我想以这样一种方式制作它,即当触摸移动很小(例如 10 像素)时,起点(fromPoint)将跟随触摸而不是停留在第一次触摸。

- (void)drawRect:(CGRect)rect {

    CGPoint fromPoint;
    CGPoint toPoint;

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 2.0);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
    CGColorRef color = CGColorCreate(colorspace, components);
    CGContextSetStrokeColorWithColor(context, color);
    CGContextSetLineCap(context, kCGLineCapRound);

    CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
    CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
    CGContextStrokePath(context);

    CGColorSpaceRelease(colorspace);
    CGColorRelease(color);

}


//touch event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch* touch = [touches anyObject];
    fromPoint = [touch locationInView:self];
    toPoint = [touch locationInView:self];

    [self setNeedsDisplay];
}

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

    UITouch* touch = [touches anyObject];
    toPoint = [touch locationInView:self];

    // ***** not working ******
    // when touch movement is small (e.g 10 pixel), the starting point will follow the touch rather than staying at the first touch 
    if (fabs(_draw.toPoint.x - _draw.fromPoint.x) < 10 && fabs(_draw.toPoint.y - _draw.fromPoint.x) < 10){

    UITouch* touch = [touches anyObject];
    fromPoint = [touch locationInView:self];
    toPoint = [touch locationInView:self];

    };

    //************************//

    [self setNeedsDisplay];  

}


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

    UITouch* touch = [touches anyObject];
    toPoint = [touch locationInView:self];

    [self setNeedsDisplay];

}

更新:目的是在实施放大镜时对起点进行轻微调整。

4

1 回答 1

0

它不起作用,因为除非您非常快速地移动手指,否则您无法区分小动作和大动作。

您可以尝试在 touchesBegan 中启动一个计时器,并使用它来计算移动速度,从中您可以在慢速和快速触摸之间设置一些参数。

您甚至可以使用 aUIPanGestureRecognizer来计算速度。

于 2013-05-08T07:30:19.327 回答