iOS:撤消绘画应用程序

发布于 2024-12-21 15:33:17 字数 1571 浏览 1 评论 0 原文

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

    lastPoint = [touch locationInView:draw];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:draw];

    UIGraphicsBeginImageContext(draw.frame.size);
    [draw.image drawInRect:CGRectMake(0, 0, draw.frame.size.width, draw.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), size);

    //eraser
    if (lastColor == 17){
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 0);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
    }

    else CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, a);

        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        draw.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        lastPoint = currentPoint;

        mouseMoved++;

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

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

    lastPoint = [touch locationInView:draw];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:draw];

    UIGraphicsBeginImageContext(draw.frame.size);
    [draw.image drawInRect:CGRectMake(0, 0, draw.frame.size.width, draw.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), size);

    //eraser
    if (lastColor == 17){
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 0);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
    }

    else CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, a);

        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        draw.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        lastPoint = currentPoint;

        mouseMoved++;

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文