2013-05-08 60 views
-1

我正在绘制一条直线,我想以这样一种方式制作,即当触摸移动很小时(例如10个像素),起点(从Point开始)将遵循触摸比留在第一次触摸。移动一条线的起点

- (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]; 

} 

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

回答

0

这不是因为除非你移动你的手指非常快的工作,你是不是能小的动作和大动作之间进行区分。

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

你甚至可以使用UIPanGestureRecognizer将制定出速度为您服务。

+0

任何参考/教程如何获得运动的速度? – askingtoomuch 2013-05-08 09:04:24