开发者

UItextview with Finger gesture recognizer

开发者 https://www.devze.com 2023-03-10 21:45 出处:网络
I am creating app to write notes with out keyboard.. I am able to draw shapes on screen with finger..But I am saving notes as image...Can I Add this same feature for UITextview??? Means I want to writ

I am creating app to write notes with out keyboard.. I am able to draw shapes on screen with finger..But I am saving notes as image...Can I Add this same feature for UITextview??? Means I want to write with finger movement in UItextview.. and want to save it in textfile...Is it possible?

Code for this

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


    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locatio开发者_如何学运维nInView: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();
    }

}


Theorically, since UITextView is a subclass of UIResponder you should be able to override methods like the one you shown.

However you should try it by yourself because the last time I tried (In iOS 3.0) there were some methods that were not called (UITextView implementation changed a lot between 2.x and 3.0) It seemed to me that UITextView was hijacking some UIResponder methods and won't let the user use them. For example touchEnded:withEvent: and touchCancelled:withEvent: were not called as in other UIResponder subclasses.

Also, textview is a subclass of UIScrollView and your drawings could be scrolled so probably you want to draw in another view or layer rather than the textView

0

精彩评论

暂无评论...
验证码 换一张
取 消