2013-08-31 76 views
8

我正在創建一個應用程序,其中當我在屏幕上滑動手指時,那時我正在使用代碼繪製線條。iOS在滑動手指上滑動手指並按下滑動路徑

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(),3.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 0.6, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), startPoint.x, startPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), endPoint.x, endPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
} 

而且我也是在同一時間在使用該行代碼運動的箭頭....

-(void)moveBallConstantly 
{ 
[UIView animateWithDuration:0.01f animations: ^{ 
     [appDel.ballImageView setCenter:CGPointMake(appDel.ballImageView.center.x +  (x/increamentFraction), appDel.ballImageView.center.y + (y/increamentFraction))]; 
    }]; 
} 

它只是功能少部分。我可以不斷移動箭頭,但爲了更好地平穩移動箭頭,我使用計時器.01重複地調用此函數。

因爲我正在做兩個處理在一起,所以它的創建問題有時。有時候箭頭移動的方法會延遲,有時候線路的移動方法會延遲。

Plz告訴我兩種方法一起工作的解決方案。

Thanx提前。

回答

0

我會報廢定時器,並將其移動到touchesMoved

我的解決方案允許您在UIImageView畫布上繪圖,並在繪圖時有一個白色框跟隨您的手指。拖放到任意的UIView嘗試一下:

// These should probably be @properties 
static CGPoint lastPoint; 
static CGPoint currentPoint; 
static UIView *box; 
static UIImageView *canvas; 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    currentPoint = [touch locationInView:self]; 
    lastPoint  = [touch locationInView:self]; 

    // Create the canvas the first time. Should probably do this elsewhere 
    // but done here for paste-ability 
    if (canvas == nil) 
    { 
     canvas = [[UIImageView alloc] initWithFrame:self.frame]; 
     canvas.backgroundColor = [UIColor redColor]; 
     [self addSubview:canvas]; 
    } 

    // Create the box that follows the finger. Should probably do this elsewhere 
    // but done here for paste-ability 
    if (box == nil) 
    { 
     box = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
     box.backgroundColor = [UIColor whiteColor]; 
     [self addSubview:box]; 
    } 

    // Ensure we can see it and move it right away 
    box.alpha = 1.0f; 
    box.center = CGPointMake(currentPoint.x, currentPoint.y - 50); 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    currentPoint = [touch locationInView:self]; 

    // Set up everything for drawing 
    UIGraphicsBeginImageContext(canvas.frame.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [canvas.image drawInRect:CGRectMake(0, 0, canvas.image.size.width, canvas.image.size.height)]; 
    CGContextSetLineCap(context, kCGLineCapRound); 
    CGContextSetLineWidth(context, 1); 
    CGContextSetStrokeColorWithColor (context, [UIColor blueColor].CGColor); 

    // Draw the path 
    CGContextBeginPath(context); 
    CGContextMoveToPoint(context, lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y); 
    CGContextStrokePath(context); 
    canvas.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    // Animate the box very quickly to the new location 
    [UIView animateWithDuration:0.1f 
          delay:0 
         options:UIViewAnimationOptionCurveEaseInOut 
        animations:^{ 
         box.center = CGPointMake(box.center.x + (currentPoint.x - lastPoint.x), 
               box.center.y + (currentPoint.y - lastPoint.y)); 
        } 
        completion:nil]; 

    // Remember our last touch 
    lastPoint = currentPoint; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // Finish it off by fading out the box 
    [UIView animateWithDuration:0.4f animations:^{ 
     box.alpha = 0.0f; 
    }]; 
} 
+0

它將汲取了手指招行,但我還需要移動箭頭在同一時間上線,所以你不認爲他們會concide與彼此。爲了移動箭頭,我使用了定時器,每調用0.01秒。 – vntstudy

+0

難題,原來的問題沒有提到保持箭頭上線:) 要做到這一點,只需將box.center設置爲等於lastPoint。如果你不想要一個輕微的動畫,你可以擺脫touchesMoved:withEvent:中的動畫塊。 –