Objective C:在 UIScrollView 中触摸时绘制(写入)

发布于 2024-12-28 17:35:06 字数 2594 浏览 0 评论 0原文

我在 TouchMoved 上有这段代码,但像其他代码一样,它在 UIScrollView 上不起作用

这是在我的 TouchMoved 上:

touchSwiped = YES;

    currentTouch = [touch locationInView:self.view];
    currentTouch.y -= 5;

    UIGraphicsBeginImageContext(self.view.frame.size);
    [writeView.image drawInRect:CGRectMake(0, 0, 768, 1024)];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, 15);
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGContextMoveToPoint(context, endPoint.x, endPoint.y);
    CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
    CGContextStrokePath(context);
    writeView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    endPoint = currentTouch;

    touchMoved++;

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

所以,我使用手势识别器传输它,但它仍然不起作用。

我在这里使用了 PanGestureRecognizer

- (void) writePan:(UIPanGestureRecognizer *)writingRecognizerP {
    switch (writingRecognizerP.state)
    {
        case UIGestureRecognizerStateChanged:

            [scrollView setScrollEnabled:NO];
            [scrollView setUserInteractionEnabled:NO];

            touchSwiped = YES;

            currentTouch = [writingRecognizerP locationInView:scrollView];
            currentTouch.y -= 5;

            UIGraphicsBeginImageContext(self.view.frame.size);
            [writeView.image drawInRect:CGRectMake(0, 0, 768, 1024)];

            CGContextRef context = UIGraphicsGetCurrentContext();

            CGContextSetLineCap(context, kCGLineCapRound);
            CGContextSetLineWidth(context, 15);
            CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
            CGContextSetBlendMode(context, kCGBlendModeNormal);
            CGContextMoveToPoint(context, endPoint.x, endPoint.y);
            CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
            CGContextStrokePath(context);
            writeView.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            endPoint = currentTouch;

            touchMoved++;

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


            break;

        case UIGestureRecognizerStateEnded:
            [scrollView setScrollEnabled:YES];
            [scrollView setUserInteractionEnabled:YES];
            break;

    }

}

任何人都知道我如何能够在触摸上书写?

我们将不胜感激! :)

I have this code on my touchesMoved but like the others its not working on UIScrollView

Here it is, on my touchesMoved:

touchSwiped = YES;

    currentTouch = [touch locationInView:self.view];
    currentTouch.y -= 5;

    UIGraphicsBeginImageContext(self.view.frame.size);
    [writeView.image drawInRect:CGRectMake(0, 0, 768, 1024)];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, 15);
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGContextMoveToPoint(context, endPoint.x, endPoint.y);
    CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
    CGContextStrokePath(context);
    writeView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    endPoint = currentTouch;

    touchMoved++;

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

so, i transferred it using Gesture recognizer but still it is not working.

i used the PanGestureRecognizer

here:

- (void) writePan:(UIPanGestureRecognizer *)writingRecognizerP {
    switch (writingRecognizerP.state)
    {
        case UIGestureRecognizerStateChanged:

            [scrollView setScrollEnabled:NO];
            [scrollView setUserInteractionEnabled:NO];

            touchSwiped = YES;

            currentTouch = [writingRecognizerP locationInView:scrollView];
            currentTouch.y -= 5;

            UIGraphicsBeginImageContext(self.view.frame.size);
            [writeView.image drawInRect:CGRectMake(0, 0, 768, 1024)];

            CGContextRef context = UIGraphicsGetCurrentContext();

            CGContextSetLineCap(context, kCGLineCapRound);
            CGContextSetLineWidth(context, 15);
            CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
            CGContextSetBlendMode(context, kCGBlendModeNormal);
            CGContextMoveToPoint(context, endPoint.x, endPoint.y);
            CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
            CGContextStrokePath(context);
            writeView.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            endPoint = currentTouch;

            touchMoved++;

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


            break;

        case UIGestureRecognizerStateEnded:
            [scrollView setScrollEnabled:YES];
            [scrollView setUserInteractionEnabled:YES];
            break;

    }

}

anyone who has an idea how will i able to write on touch??

it'll be much appreciated! :)

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

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人心善变 2025-01-04 17:35:06

您应该在 -drawRect: 中完成所有绘图,并且当发生更改时发送您的视图 -setNeedsDisplaysetNeedsDisplayInRect: 消息。

You should do all your drawing in -drawRect:, and when something changes send your view -setNeedsDisplay or setNeedsDisplayInRect: message.

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