2013-08-31 17 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キャンバスに描画することができます。また、描画中に指先に白いボックスが表示されます。彼らはconcideだろうと思いますこれは、指の動きで線を描画します

// 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

、しかし、私はライン上で同時にまた、矢印を移動する必要がある、だから、あなたはいけない:それを試してみるために、任意のUIViewにドロップしますお互いに。矢印を動かすために、私は0.01秒ごとに呼ばれるタイマーを使用しています。 – vntstudy

+0

Gotcha、元の質問は、行に矢印を残しているとは言及していません:) これを行うには、box.centerをlastPointと等しく設定します。わずかなアニメーションが必要ない場合は、touchesMoved:withEvent:のアニメーションブロックを取り除くことができます。 –

関連する問題