2013-05-08 62 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