2011-06-04 126 views
0

我正在创建应用程序来写出笔记与键盘..我可以在屏幕上用手指画出形状..但我将笔记保存为图像...我可以为UITextview添加相同的功能???意思是我想在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

Theorically,因为UITextViewUIResponder一个子类,你应该能够覆盖像你显示的一个方法。

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

此外,TextView的是UIScrollView一个子类,你的图纸也可以这么大概滚动要在其他视图中绘制或层,而不是TextView的

+0

确定..你的意思是ImageView的,给这个目的是什么? – DeviPhone26 2011-06-04 10:53:08

+0

如果你正在绘制东西,那么UIView就足够了。 UIImageView是用于显示图像文件:)只要尝试UITextView,如果您认为太难以处理绘图+滚动,那么只需使用UIView作为画布。 – nacho4d 2011-06-04 12:53:41