【问题标题】:UItextview with Finger gesture recognizer带有手指手势识别器的 UItextview
【发布时间】:2011-06-04 18:47:09
【问题描述】:

我正在创建应用程序来编写没有键盘的笔记..我可以用手指在屏幕上绘制形状..但是我将笔记保存为图像...我可以为 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();
    }

}

【问题讨论】:

    标签: iphone uitextview uigesturerecognizer


    【解决方案1】:

    理论上,由于UITextViewUIResponder 的子类,您应该能够覆盖您展示的方法。

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

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

    【讨论】:

    • 如果你正在画东西,那么 UIView 就足够了。 UIImageView 用于显示图像文件 :) 尝试 UITextView,如果您认为处理绘图和滚动太难,那么只需使用 UIView 作为您的画布。
    猜你喜欢
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多