UItextview与手指手势识别器

时间:2011-06-04 10:12:17

标签: iphone uitextview uigesturerecognizer

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

}

1 个答案:

答案 0 :(得分:1)

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

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

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