2012-05-09 2 views
0

Je suis en train de développer une application iOs 5 dans laquelle vous pouvez peindre sur un UIImageView à l'intérieur d'une sous-vue de la vue principale. Lorsque vous peignez le premier 30px de la ligne ne suit pas votre coup. Mais ce qui est vraiment rare, c'est que si vous taillez le ImageView et faites sa taille comme tout l'écran 460x320 (bien que vous ne voyez pas le plein ImageView parce que c'est plus grand que la vue où il est à l'intérieur.) Cela fonctionne bien. Alors peut-être a une solution. C'est mon code:Erreur de peinture sur l'application iOS

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    mouseSwiped = NO; 
    UITouch *touch = [touches anyObject]; 

    if ([touch tapCount] == 2) { 
     drawImage.image = nil; 
     return; 
    } 

    lastPoint = [touch locationInView:self.view]; 
    lastPoint.y -= 10; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    mouseSwiped = YES; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.drawImage]; 
    currentPoint.y -= 10; // only for 'kCGLineCapRound' 
    UIGraphicsBeginImageContext(self.drawImage.frame.size); 

    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5); // for size 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0); //values for R, G, B, and Alpha 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 

    mouseMoved++; 

    if (mouseMoved == 10) { 
     mouseMoved = 0; 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {  
    UITouch *touch = [touches anyObject]; 

    if ([touch tapCount] == 2) { 
     drawImage.image = nil; 
     return; 
    } 
    if(!mouseSwiped) { 
     //if color == green 
     UIGraphicsBeginImageContext(self.drawImage.frame.size); 
     [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0); 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     CGContextFlush(UIGraphicsGetCurrentContext()); 
     drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 
} 

That's what I get

Répondre

1

Le problème est que la première touchesMoved: est retardée par l'API. Je ne suis pas sûr que quelqu'un ait pu contourner ce problème pour le moment.

S'il vous plaît déposer un rapport de bogue à http://radar.apple.com

Le vrai problème dans votre cas est une simple erreur, si: Vous utilisez deux systèmes de coordonnées différents dans touchesBegan: et touchesMoved:.

touchesBegan:

lastPoint = [touch locationInView:self.view]; 
           ////////// 

touchesMoved:

CGPoint currentPoint = [touch locationInView:self.drawImage]; 
              /////////////// 
+0

Désolé, je n'ai pas fini la phrase, lire maintenant. :) –

+0

Désolé, maintenant j'ai terminé la phrase :) Lisez-le maintenant, je pense que vous comprendrez! –

+0

@MartiSerraVivancos Veuillez déposer un rapport de bogue à propos de ce comportement. Apple ne va probablement pas le réparer si pas assez de gens se plaignent. – fzwo