作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我编写了以下代码,用于使用贝塞尔路径绘制文本。
现在我的任务是我们如何识别使用 Beizer 路径绘制的文本。
例如,如果我画“苹果”,那么我的标签将显示“你画了苹果”。
@implementation NaiveVarWidthView
{
UIBezierPath *path;
UIImage *incrementalImage;
CGPoint pts[5];
uint ctr;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setMultipleTouchEnabled:NO];
path = [UIBezierPath bezierPath];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
ctr = 0;
UITouch *touch = [touches anyObject];
pts[0] = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
ctr++;
pts[ctr] = p;
if (ctr == 4)
{
pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0);
[path moveToPoint:pts[0]];
[path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]];
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0); //
if (!incrementalImage)
{
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds];
[[UIColor whiteColor] setFill];
[rectpath fill];
}
[incrementalImage drawAtPoint:CGPointZero];
[[UIColor blackColor] setStroke];
float speed = 0.0;
for (int i = 0; i < 3; i++)
{
float dx = pts[i+1].x - pts[i].x;
float dy = pts[i+1].y - pts[i].y;
speed += sqrtf(dx * dx + dy * dy);
}
#define FUDGE_FACTOR 100
float width = FUDGE_FACTOR/speed;
[path setLineWidth:width];
[path stroke];
incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setNeedsDisplay];
[path removeAllPoints];
pts[0] = pts[3];
pts[1] = pts[4];
ctr = 1;
}
}
- (void)drawRect:(CGRect)rect
{
[incrementalImage drawInRect:rect];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self setNeedsDisplay];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
@end
最佳答案
我不确定您是否有兴趣使用开源代码,但您可以尝试一下 GLGestureRecognizer .
这是对您在 stack-Overflow 中的问题的一个这样的回答。
关于ios - 识别使用贝塞尔路径绘制的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20536030/
我是一名优秀的程序员,十分优秀!