How to draw a signature with lines on the iPhone screen?

I want the user to draw a signature on the iPhone screen, so I add a subclass of UIView and add some code to my "touchhesMoved" method.

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; firstTouch = [touch locationInView:self]; CGSize mySize = CGSizeMake(5, 5); UIGraphicsBeginImageContext(mySize); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextBeginPath(ctx); CGContextSetRGBFillColor(ctx, 1, 0, 0, 1); CGContextAddRect(ctx, CGRectMake(0, 0, 5, 5)); CGContextFillPath(ctx); UIImage *redRect = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageView *redRectView = [[UIImageView alloc] initWithImage:redRect]; redRectView.center = CGPointMake(firstTouch.x, firstTouch.y); [self addSubview:redRectView]; } 

I draw it in small rectangles and it turns out to be accurate. Since this is too ugly, I want to draw the signature in lines. But how to distinguish between firstTouch and lastTouch? If I use the "touchsMoved" method, I can only get one touch point.

+2
source share
3 answers

According to the UIResponder class reference, you will also need to implement – touchesBegan:withEvent: and
– touchesEnded:withEvent:
After implementing these methods, you can get enough data to implement a bezier curve or other suitable solution.

[ edit ] Perhaps the best solution would be to get the strokes directly from the UIEvent object as soon as your controller receives a – touchesMoved:withEvent: notification. In addition, a sample GLPaint sample may also be useful.

+3
source

Since the GLPaint example code may be too complicated for beginners, I find this tutorial . This is easy to learn for most beginners.

+2
source

Keep in mind that a signature written with one finger will differ from that written with a writing tool. You might want to rethink what you are using the signature for, and whether it is mandatory as you need.

0
source

Source: https://habr.com/ru/post/893254/


All Articles