书法绘图IOS核心 [英] Calligraphy Drawing IOS Coregraphics

查看:221
本文介绍了书法绘图IOS核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要有一个标记为红色的东西,作为标记,但不知道是否可以做。



它看起来像标记书法笔。



>



尝试以下代码作为回答Andrey,我在绘制时获得以下输出结果。





可以在这里找到画刷图片



进一步更改代码,我发现仍然无法获得预期的结果。
这里我试图填充当前点和下一个点之间存在的路径。

   - )drawRect:(CGRect)rect 
{
//绘图代码


CGFloat w = 5.0;
CGFloat h = 2.0;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,[UIColor redColor] .CGColor);

for(int i = 0; i< self.positions.count; i ++){
CGPoint point = [[self.positions objectAtIndex:i] CGPointValue];

CGFloat x = point.x-(w / 2.0);
CGFloat y = point.y-(h / 2.0);
CGContextFillRect(context,CGRectMake(x,y,w,h));
if(i + 1< self.positions.count){
CGPoint nextPoint = [[self.positions objectAtIndex:(i + 1)] CGPointValue];
CGMutablePathRef myPath = CGPathCreateMutable();
CGPathMoveToPoint(myPath,NULL,x,y);
CGFloat x1 = nextPoint.x-(w / 2.0);
CGFloat y1 = nextPoint.y-(h / 2.0);

CGPathAddLineToPoint(myPath,NULL,x1,y1);
CGPathAddLineToPoint(myPath,NULL,w,y1);
CGPathAddLineToPoint(myPath,NULL,w,y);
CGPathCloseSubpath(myPath);
CGContextFillPath(context);
}
}
}


解决方案>

这些图片可以通过在 UIView 子类中重写 drawRect:消息来绘制图片。你还需要添加一些触摸处理程序来捕捉触摸。所以这里是一些代码:

  @interface DrawingView()

@property(nonatomic,strong) NSMutableArray * positions;

@end

@implementation DrawingView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self){
[self awakeFromNib];
}
return self;
}

- (void)awakeFromNib
{
self.positions = [[NSMutableArray alloc] init];
}

- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];

UIImage * brushImage = [UIImage imageNamed:@brush.png];
for(NSValue * object in self.positions){
CGPoint point = [object CGPointValue];
[brushImage drawAtPoint:CGPointMake(point.x - brushImage.size.width / 2.0,point.y - brushImage.size.height / 2.0)];
}
}

- (void)addPoint:(CGPoint)point
{
[self.positions addObject:[NSValue valueWithCGPoint:point] ;

[self setNeedsDisplay];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
[self addPoint:[touch locationInView:self]];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
[self addPoint:[touch locationInView:self]];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
[self addPoint:[touch locationInView:self]];
}

@end

您只需创建 brush.png ,并将这些视图放在任何你想要的地方。


I am looking to have something which is marked in red, as marker but don't know whether it can be done or not.

It looks like marking using a calligraphy pen.

Trying out the following code as answered by "Andrey", I get the following output with spaces while drawing.

Brush image can be found here used is

Making further changes to the code, I found still I am not able to get the expected result. Here I am trying to fill the path which exists between the current point and the next point.

- (void)drawRect:(CGRect)rect
{
    // Drawing code


    CGFloat w=5.0;
    CGFloat h=2.0;
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);

    for(int i=0;i<self.positions.count;i++){
        CGPoint point=[[self.positions objectAtIndex:i] CGPointValue];

        CGFloat x=point.x-(w/2.0);
        CGFloat y=point.y-(h/2.0);
        CGContextFillRect(context, CGRectMake(x, y, w, h));
        if(i+1<self.positions.count){
            CGPoint nextPoint=[[self.positions objectAtIndex:(i+1)] CGPointValue];
            CGMutablePathRef myPath=CGPathCreateMutable();
            CGPathMoveToPoint(myPath, NULL, x, y);
            CGFloat x1=nextPoint.x-(w/2.0);
            CGFloat y1=nextPoint.y-(h/2.0);

            CGPathAddLineToPoint(myPath, NULL, x1, y1);
            CGPathAddLineToPoint(myPath, NULL, w, y1);
            CGPathAddLineToPoint(myPath, NULL, w, y);
            CGPathCloseSubpath(myPath);
            CGContextFillPath(context);
        }
    }
}

解决方案

Those picture can be done via drawing an image in UIView subclass with overridden drawRect: message. You also need to add some kind of touch handler for catching touches. So here is some code:

@interface DrawingView ()

@property (nonatomic, strong) NSMutableArray *positions;

@end

@implementation DrawingView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self awakeFromNib];
    }
    return self;
}

- (void)awakeFromNib
{
    self.positions = [[NSMutableArray alloc] init];
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    UIImage *brushImage = [UIImage imageNamed:@"brush.png"];
    for (NSValue *object in self.positions) {
        CGPoint point = [object CGPointValue];
        [brushImage drawAtPoint:CGPointMake(point.x - brushImage.size.width / 2.0, point.y - brushImage.size.height / 2.0)];
    }
}

- (void)addPoint:(CGPoint)point
{
    [self.positions addObject:[NSValue valueWithCGPoint:point]];

    [self setNeedsDisplay];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    [self addPoint:[touch locationInView:self]];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    [self addPoint:[touch locationInView:self]];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    [self addPoint:[touch locationInView:self]];
}

@end

You only need to create a proper brush.png and place those view anywhere you want.

这篇关于书法绘图IOS核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆