2013-08-31 11 views
8

Sto creando un'app, in cui quando faccio scorrere il dito sullo schermo, quella volta disegno la linea usando il codice.iOS Disegna linea su Swiping Finger con Arrow seguendo il percorso Swipe

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

e sono anche in movimento freccia al tempo stesso in quella riga utilizzando il codice ....

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

suo solo piccola parte delle funzioni. Sono in grado di muovere la freccia costantemente, ma per un migliore movimento della freccia, sto chiamando ripetutamente questa funzione con il timer 01.

Come sto facendo entrambe le elaborazioni insieme quindi il problema di creazione a volte. A volte il metodo di spostamento della freccia viene ritardato e talvolta il metodo di avanzamento della linea viene ritardato.

Plz dimmi che la soluzione per fare entrambe le cose funziona insieme.

Grazie in anticipo.

risposta

0

Vorrei scartare il timer e spostarlo in touchesMoved.

La mia soluzione consente di disegnare su una tela UIImageView e ha una casella bianca che segue il dito mentre si disegna. Rilasciarlo in qualsiasi UIView per provarlo:

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

sarà tracciare la linea con la mossa dito, ma ho bisogno di spostare la freccia anche allo stesso tempo sulla linea, in modo da non pensare che essi concide insieme. Per spostare la freccia, sto usando il timer, che viene chiamato ogni .01 secondi. – vntstudy

+0

Gotcha, la domanda originale non ha menzionato il mantenimento della freccia sulla linea :) Per fare ciò basta impostare box.center per essere uguale all'ultimo punto. Se non vuoi una leggera animazione, puoi semplicemente sbarazzarti del blocco dell'animazione nei touchMoved: withEvent :. –