0

私はiPhone開発に不慣れです。指で写真を描く必要があるタスクがあります。派手なものは何もありません。1つの幅(シガンチャー)の1つの色だけです。誰かが私にこれを実装する最良の方法を教えてもらえますか?tnx

4

2 に答える 2

0

私はあなたが望むデモを作成するので、以下はそのためのコードです。

CGPoint midPoint(CGPoint p1, CGPoint p2)
{
    return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);
}


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

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    UITouch *touch  = [touches anyObject];
    
    previousPoint2  = previousPoint1;
    previousPoint1  = [touch previousLocationInView:self];
    currentPoint    = [touch locationInView:self];
    
    // calculate mid point
    CGPoint mid1    = midPoint(previousPoint1, previousPoint2); 
    CGPoint mid2    = midPoint(currentPoint, previousPoint1);
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, mid1.x, mid1.y);
    CGPathAddQuadCurveToPoint(path, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
    CGRect bounds = CGPathGetBoundingBox(path);
    CGPathRelease(path);
    
    CGRect drawBox = bounds;
    
    //Pad our values so the bounding box respects our line width
    drawBox.origin.x        -= self.lineWidth * 2;
    drawBox.origin.y        -= self.lineWidth * 2;
    drawBox.size.width      += self.lineWidth * 4;
    drawBox.size.height     += self.lineWidth * 4;
    
    UIGraphicsBeginImageContext(drawBox.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    curImage = UIGraphicsGetImageFromCurrentImageContext();
    [curImage retain];
    UIGraphicsEndImageContext();
    
    [self setNeedsDisplayInRect:drawBox];
    
}

- (void)drawRect:(CGRect)rect
{
    
    [curImage drawAtPoint:CGPointMake(0, 0)];
    CGPoint mid1 = midPoint(previousPoint1, previousPoint2); 
    CGPoint mid2 = midPoint(currentPoint, previousPoint1);

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    
    [self.layer renderInContext:context];

    CGContextMoveToPoint(context, mid1.x, mid1.y);
    CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y); 
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, self.lineWidth);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);

    CGContextStrokePath(context);

    [super drawRect:rect];
    
    [curImage release];
    
}

こちらのソースコードもダウンロードしてください。

これはとても役に立ったと思います。

ハッピーコーディング。

于 2012-08-14T13:39:04.380 に答える
0

このリンクを参照してください....この質問は閉じられていますが、解決策は正しいです。 https://stackoverflow.com/questions/11828200/writing-with-finger-on-iphone/11828810#11828810

このコードを見てください。私はUITouchを使用しました

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

 }

 // Handles the continuation of a touch.
 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
 mouseSwiped = YES;

 UITouch *touch = [[event touchesForView:baseView] anyObject];
CGPoint currentPoint = [touch locationInView:drawingView];
//currentPoint.y -= 20;

UIGraphicsBeginImageContext(drawingView.frame.size);
[drawingView.image drawInRect:CGRectMake(0, 0, drawingView.frame.size.width, drawingView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext( ), 5.0);
if (eraserSelected)
{
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext( ), 10.0);
}
[self changeBrushColor];
//[self changeBrushColor:segmentedControl];
//CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext() , lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext()) ;
drawingView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

lastPoint = currentPoint;
}

 // Handles the end of a touch event when the touch is a tap.
 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
 {
//UITouch *touch = [[event touchesForView:self.view] anyObject];
/*
 if ([touch tapCount] == 2)
 {
 imgView.image = nil;
 return;
 }
 */

if(!mouseSwiped) {
    UIGraphicsBeginImageContext(drawingView.frame.size);
    [drawingView.image drawInRect:CGRectMake(0, 0, drawingView.frame.size.width, drawingView.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext( ), 5.0);
    [self changeBrushColor];
    //[self changeBrushColor:segmentedControl];
    //CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext() , lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext()) ;
    CGContextFlush(UIGraphicsGetCurrentContext());
    drawingView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}
}

- (void)changeBrushColor
 {
switch (selectedColor) {
    case 1:
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
        break;
    case 2:
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.45, 0.87, 0.04,            1.0);
        break;
于 2012-08-14T13:18:40.413 に答える