2011-06-04 173 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