繁体   English   中英

带有手指手势识别器的 UItextview

[英]UItextview with Finger gesture recognizer

我正在创建应用程序来编写没有键盘的笔记..我可以用手指在屏幕上绘制形状..但是我将笔记保存为图像...我可以为 UITextview 添加相同的功能吗? 意味着我想在 UItextview 中用手指移动写字……并想将其保存在文本文件中……有可能吗?

为此的代码

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


    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.y -=20;

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

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    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());

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

    lastPoint = currentPoint;

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

}

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


    UITouch *touch  = [touches anyObject];

    if ([touch tapCount] ==2 ) {
        drawImage.image = nil;
        return ;
    }

    if (!mouseSwiped) {

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

        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);

        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0);

        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);

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

}

从理论上讲,由于UITextViewUIResponder的子类,因此您应该能够覆盖您显示的方法。

但是您应该自己尝试一下,因为我上次尝试(在 iOS 3.0 中)有一些方法没有被调用( UITextView实现在 2.x 和 3.0 之间发生了很大变化)在我看来UITextView正在劫持一些UIResponder方法并且不会让用户使用它们。 例如touchEnded:withEvent:touchCancelled:withEvent:没有像在其他UIResponder子类中那样被调用。

此外,textview 是UIScrollView的子类,您的绘图可以滚动,因此您可能想在另一个视图或图层中绘制而不是 textView

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM