触摸时绘制直线移动IOS

时间:2013-05-08 07:14:07

标签: iphone ios ipad core-graphics drawrect

我计划做简单的绘图,使用它可以绘制直线和自由手绘,根据它改变的选择。但问题是直线从起点重复超过10或15行,当我尝试绘制1行,自由手绘图工作正常。我在这里发布了我的代码。请提出解决此问题的建议。

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


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    // Work download Flag
        _isWorkModified = YES;

    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];
        previousPoint2 = previousPoint1;
    previousPoint1 = [touch previousLocationInView:self.view];
    currentPoint = [touch locationInView:self.view];

    UIGraphicsBeginImageContext(self.view.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    if(_selectedShape == 3)
    {
        // calculate mid point
        CGPoint mid1 = [self midPoint:previousPoint1 :previousPoint2];
        CGPoint mid2 = [self midPoint:currentPoint :previousPoint1];

        CGContextMoveToPoint(context, mid1.x, mid1.y);
        CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
        CGContextSetLineCap(context, kCGLineCapRound);
        if(self.isEraseButtonSelected)
        {
            CGContextSetLineWidth(context, 40.0);
            CGContextSetRGBStrokeColor(context, 0.823,0.886,0.898,1.0);
        }
        else
        {
            CGContextSetLineWidth(context, 3.0);
            CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
        }
        CGContextStrokePath(context);
    }
    else if(_selectedShape == 0)
    {
        CGContextSetLineWidth(context, 3.0);
        CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);

        CGContextClosePath(context);
        CGContextStrokePath(context);

    }
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

        NSLog(@"touch ended");
        if(!mouseSwiped)
        {
            NSInteger index = [[[AnswerDataHandler getAnswerDataHandler]workAreaArray] indexOfObject:self];
            [[ImageStoreHelper getImageStoreHelper] saveImageForQuestionNumber:index image:drawImage.image];
        }

    }


 }

0 个答案:

没有答案