2013-08-31 16 views
8

Ich erstelle eine App, in der, wenn ich meinen Finger auf dem Bildschirm wische, dieses Mal zeichne ich Linie mit Code.iOS Ziehe Linie auf Wischen Finger mit Pfeil folgt dem Swipe-Pfad

- (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()); 
} 

Und ich bin auch zugleich an dieser Zeile Code ....

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

Es ist nur wenig Teil der Funktion zu bewegen Pfeil. Ich bin in der Lage, den Pfeil ständig zu bewegen, aber für eine bessere sanfte Bewegung des Pfeils rufe ich diese Funktion wiederholt mit dem Timer .01 auf.

Da mache ich beide die Verarbeitung zusammen, so dass es manchmal zu schaffen Problem. Manchmal wird die Methode der Pfeilbewegung verzögert und manchmal verzögert sich die Linienmethode.

Plz sagen Sie mir die Lösung, beide zusammen zu tun.

Vielen Dank im Voraus.

Antwort

0

Ich würde den Timer verschrotten und es in touchesMoved verschieben.

Mit meiner Lösung können Sie auf einem UIImageView-Zeichenbereich zeichnen und beim Zeichnen eine weiße Box Ihrem Finger folgen lassen. Drop in jede UIView, um es auszuprobieren:

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

Es wird die Zeile mit dem Finger bewegen ziehen, aber ich brauche den Pfeil auch zugleich auf der Linie zu bewegen, also Sie nicht denken, dass sie concide miteinander. Zum Bewegen des Pfeils verwende ich einen Timer, der alle 0,01 Sekunden aufgerufen wird. – vntstudy

+0

Gotcha, ursprüngliche Frage nicht erwähnt, den Pfeil auf der Linie zu halten :) Um dies zu tun, nur die box.center gleich dem lastPoint sein. Wenn Sie keine leichte Animation wünschen, können Sie den Animationsblock in touchesMoved: withEvent: einfach loswerden. –