objective-c - Objective-C : Draw(Write) on Touch in a UIScrollView

标签 objective-c ios uiscrollview uigesturerecognizer

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

在我的 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;
    }

因此,我使用手势识别器传输了它,但它仍然无法正常工作。

我使用了 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;

    }

}

有人知道我怎样才能用触摸写字吗??

我们将不胜感激! :)

最佳答案

您应该在 -drawRect: 中完成所有绘图,当某些内容发生变化时,请发送您的 View -setNeedsDisplaysetNeedsDisplay InRect: 消息。

关于objective-c - Objective-C : Draw(Write) on Touch in a UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8999027/

相关文章:

ios - 随时恢复 UILocalNotification 或远程推送通知

android - 移动网站比设备宽,捏一下就可以了

ios - 自动旋转设备时 UIScrollView 中的图形故障

iphone - 如何以编程方式复制界面生成器中内置的 UIView?

ios - 如何在iPhone 6/6s Plus上保持人像启动屏幕

objective-c - cocoa touch 问题。 [NSMutableArray array] 应该保留吗?

ios - UIBarButtonItem 垂直定位

objective-c - 如何隐藏 Cocoa 应用程序中的菜单栏?

iphone - 是否可以将 iOS 文件共享功能限制到文档目录中的子文件夹?

ios - UITapGestureRecognizer 无法在 UIScrollView 中工作